In: Computer Science
17.9 Worksheet 7
Follow the instructions commented into the given template. There will be no partial credit given.
#include <iostream>
using namespace std
int main()
{
int array1[20] = {3, 18, 1, 25, 4, 7, 30, 9, 80, 16, 17};
int numElements = 11;
cout << "Part 1" << endl;
// Part 1
// Enter the statement to print the numbers in index 4 and index
9
// put a space in between the two numbers
cout << endl;
// Enter the statement to print the numbers 3 and 80 from the array
above
// put a space in between the two numbers
cout << endl;
// Enter the statement to change the number 1 in the array to be
12
// then write the statement to print out that number in the
array
cout << "\nPart 2" << endl;
// Part 2
// Write a function called printAll. It takes in an array
// and an integer that has the number of values in the array.
// The function should print all the numbers in the array
with
// a space between each one.
// Call the function on the line below.
cout << "\nPart 3" << endl;
// Part 3
// Write a function called printEven. It takes in an array
and
// an integer that has the number of values in the array. It
prints
// all the even numbers in the array with a space between each
one.
// This function returns the count of evens.
int evens;
// Call the function you just wrote and store the
// answer in the variable evens declared above.
// This will print the number of evens in the array.
cout << endl << evens;
cout << "\nPart 4" << endl;
// Part 4
// Write a function called computeTotalOdds. It takes in an array
and
// an integer that has the number of values in the array. It will
return
// the total of all of the odd numbers in the array
int total;
// Call the function you just wrote and store the answer
// in the variable total declared above
// This will print the total out
cout << endl << total;
return 0;
}
//rate my solution and comment if any doubts
Note:
//please do observe the comments that i mentioned you can change your code to your convenience. I wrote 'or' in my solution
Code:
#include <iostream>
using namespace std;
int printAll(int array1[],int numElements)
{
for(int i=0;i<numElements;i++)
cout<<array1[i]<<"
";
}
int printEven(int array1[],int numElements)
{
int c=0; //store count
for(int i=0;i<numElements;i++)
{
if(array1[i]%2==0) //to check if
even
{
c++;
cout<<array1[i]<<" ";
}
}
return c;
}
int computeTotalOdds(int array1[],int numElements)
{
int c=0; //to store count
for(int i=0;i<numElements;i++)
{
if(array1[i]%2!=0) // to check if
odd
{
c++;
//cout<<array1[i]<<" ";//to print odd numbers if you
want to
}
}
return c;
}
int main()
{
int array1[20] = {3, 18, 1, 25, 4, 7, 30, 9, 80, 16, 17};
int numElements = 11;
cout << "Part 1" << endl;
// Part 1
// Enter the statement to print the numbers in index 4 and index
9
// put a space in between the two numbers
cout << array1[4] <<" "<<array1[9];
cout << endl;
// Enter the statement to print the numbers 3 and 80 from the array
above
// put a space in between the two numbers
for(int i=0;i<11;i++)
{
if(array1[i]==3 or array1[i]==80)
cout<<array1[i]<<"
";
}
/*or you can also use
cout<<array1[0]<<" "<<array1[8];
*/
cout << endl;
// Enter the statement to change the number 1 in the array to be
12
// then write the statement to print out that number in the
array
int i;
for(i=0;i<11;i++)
if(array1[i]==1)
{
array1[i]=12;
break;
}
cout<<array1[i];
/*or you can also use
array1[2]=12;
cout<<array1[2];
*/
cout << "\nPart 2" << endl;
// Part 2
// Write a function called printAll. It takes in an array
// and an integer that has the number of values in the array.
// The function should print all the numbers in the array
with
// a space between each one.
// Call the function on the line below.
printAll(array1,numElements);
cout << "\nPart 3" << endl;
// Part 3
// Write a function called printEven. It takes in an array
and
// an integer that has the number of values in the array. It
prints
// all the even numbers in the array with a space between each
one.
// This function returns the count of evens.
int evens;
evens=printEven(array1,numElements);
// Call the function you just wrote and store the
// answer in the variable evens declared above.
// This will print the number of evens in the array.
cout << endl << evens;
cout << "\nPart 4" << endl;
// Part 4
// Write a function called computeTotalOdds. It takes in an array
and
// an integer that has the number of values in the array. It will
return
// the total of all of the odd numbers in the array
int total;
total=computeTotalOdds(array1,numElements);
// Call the function you just wrote and store the answer
// in the variable total declared above
// This will print the total out
cout << endl << total;
return 0;
}
Output:
Code(Screen shots):