Need help on C++ Homework. Thanks I did the code for function 1- 4 But got confu
ID: 3589472 • Letter: N
Question
Need help on C++ Homework.
Thanks
I did the code for function 1- 4 But got confused with the rest .
Here is the code for it below
#include <iostream>
#include <stdlib.h>
using namespace std;
void printArray(int array[]) {
for (int i = 0; i < 100; ++i) {
cout << array[i] << " " << array[i + 100] << " "
<< array[i + 200] << " " << array[i + 300] << " "
<< array[i + 400] << " " << array[i + 500] << " "
<< array[i + 600] << " " << array[i + 700] << " "
<< array[i + 800] << " " << array[i + 900] << " ";
cout << endl;
}
cout << endl << endl;
}
void oddNums(int array[]) {
int oddTotal = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] % 2 == 1) {
oddTotal += array[i];
}
}
cout << "Sum of all odd number present in array = " << oddTotal << endl << endl;
}
void evenNums(int array[]) {
int evenTotal = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] % 2 == 0) {
evenTotal += array[i];
}
}
cout << "Sum of all even number present in array = " << evenTotal << endl << endl;
}
void search(int array[]) {
cout << "Enter value to search : ";
int key;
cin >> key;
int found = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] == key) {
cout << key << " found at index " << i << endl << endl;
++found;
}
}
if (found == 0) {
cout << "Value is not in array ";
}
}
int main() {
int array[1000];
for (int i = 0; i < 1000; ++i) {
array[i] = 1 + rand() % 1000;
}
int choice = 1;
while (choice) {
cout << endl;
cout << "Enter 1. Output all integer values ";
cout << "Enter 2. Sum all odd numbers ";
cout << "Enter 3. Sum all even numbers ";
cout << "Enter 4. Enter a search value ";
cout << "Enter 5. exit ";
cin >> choice;
switch (choice){
case 1:
printArray(array);
break;
case 2:
oddNums(array);
break;
case 3:
evenNums(array);
break;
case 4:
search(array);
break;
case 5:
exit(1);
break;
default:
cout << "Invalid choice !! ";
cout << "Please enter a valid choice .... ";
break;
}
}
return 0;
}
LaB aSSIGN MeNT Task 1: Main Function: Declare and fill an array with 1000 random integers between 1 - 1000. You will pass the array into functions that will perform operations on the array. Function 1 create a function that will output all the integers in the array. (Make sure output is clearly formatted. Function 2: create a Function that will sum and output all the odd numbers in the array. Function 3: create a Function that will sum and output all the even numbers in the array. Function 4: create a function that will allow the user to enter an integer value to search. Program will output if integer value is found and the location of the integer in the array. or The program wil output "value is not in array". ("value is not in array should not be printed more than once.") Function 5 create a function that will output the highest value in the array.Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
void printArray(int array[]) {
for (int i = 0; i < 100; ++i) {
cout << array[i] << " " << array[i + 100] << " "
<< array[i + 200] << " " << array[i + 300] << " "
<< array[i + 400] << " " << array[i + 500] << " "
<< array[i + 600] << " " << array[i + 700] << " "
<< array[i + 800] << " " << array[i + 900] << " ";
cout << endl;
}
cout << endl << endl;
}
void oddNums(int array[]) {
int oddTotal = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] % 2 == 1) {
oddTotal += array[i];
}
}
cout << "Sum of all odd number present in array = " << oddTotal << endl << endl;
}
void evenNums(int array[]) {
int evenTotal = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] % 2 == 0) {
evenTotal += array[i];
}
}
cout << "Sum of all even number present in array = " << evenTotal << endl << endl;
}
void search(int array[]) {
cout << "Enter value to search : ";
int key;
cin >> key;
int found = 0;
for (int i = 0; i < 1000; ++i) {
if (array[i] == key) {
cout << key << " found at index " << i << endl << endl;
++found;
}
}
if (found == 0) {
cout << "Value is not in array ";
}
}
void highest(int array[]) {
int key=-89898;
for (int i = 0; i < 1000; ++i) {
if (array[i] >key) {
key=array[i];
}
}
cout<<"The highest no is = " <<key<<endl;
}
void lowest(int array[]) {
int key=89898;
for (int i = 0; i < 1000; ++i) {
if (array[i] <key) {
key=array[i];
}
}
cout<<"The lowesst no is = " <<key<<endl;
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void sort(int arr[])
{
int i, j;
for (i = 0; i <1000; i++) {
// Last i elements are already in place
for (j = 0; j < 1000-i-1; j++) {
if (arr[j] < arr[j+1])
swap(&arr[j], &arr[j+1]);}}
printArray(arr);
}
void first(int arr[])
{
cout<<"The first element is "<<arr[0];
}
void last(int arr[])
{
cout<<"The last element is "<<arr[999];
}
int main() {
int array[1000];
for (int i = 0; i < 1000; ++i) {
array[i] = 1 + rand() % 1000;
}
int choice = 1;
while (choice) {
cout << endl;
cout << "Enter 1. Output all integer values ";
cout << "Enter 2. Sum all odd numbers ";
cout << "Enter 3. Sum all even numbers ";
cout << "Enter 4. Enter a search value ";
cout << "Enter 5. The highest value ";
cout << "Enter 6. The highest value ";
cout << "Enter 7. Sort ";
cout << "Enter 8. First element ";
cout << "Enter 9. Last element ";
cout << "Enter 10. exit ";
cin >> choice;
switch (choice){
case 1:
printArray(array);
break;
case 2:
oddNums(array);
break;
case 3:
evenNums(array);
break;
case 4:
search(array);
break;
case 5:
highest(array);
break;
case 6:
lowest(array);
break;
case 7:
sort(array);
break;
case 8:
first(array);
break;
case 9:
last(array);
break;
case 10:
exit(1);
break;
default:
cout << "Invalid choice !! ";
cout << "Please enter a valid choice .... ";
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.