Question

17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int...

17.9 Worksheet 7 (C++)

Follow the instructions commented into the given template.

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

void printAll(int arr[],int n){
   for(int i=0;i<n;i++)
       cout<<arr[i]<<" ";
   cout<<endl;
}
int printEven(int arr[], int n){
  
   int count=0;
   for(int i=0;i<n;i++){
       if(arr[i]%2==0){
           cout<<arr[i]<<" ";
           count++;
       }
      
   }
   cout<<endl;

   return count;

}
int computeTotalOdds(int arr[], int n){
  
   int total=0;
   for(int i=0;i<n;i++){
       if(arr[i]%2==1){
           total+=arr[i];
       }
      
   }
   cout<<endl;

   return total;

}
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;


cout<<array1[0]<<" "<<array1[8];

// Enter the statement to print the numbers 3 and 80 from the array above
// put a space in between the two numbers
cout << endl;

array1[2]=12;
// 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<<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 = 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=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;
}

Add a comment
Know the answer?
Add Answer to:
17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [IN JAVA] Copy the following code into your main.cpp and complete it public class Main { pub...

    [IN JAVA] Copy the following code into your main.cpp and complete it public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • In C++ First create the two text file given below. Then complete the main that is given. There ar...

    In C++ First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12...

  • First create the two text file given below. Then complete the main that is given. There...

    First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...

  • in c++ Finish the methods in the following code. DO NOT CHANGE THE MAIN METHOD. The...

    in c++ Finish the methods in the following code. DO NOT CHANGE THE MAIN METHOD. The instructions for each method are in a comment in the template. DO NOT CHANGE THE MAIN METHOD!!!! #include <iostream> #include <string> using namespace std; // prototypes int main() { int answer = 0; int value1 = 0; int value2 = 0; double average = 0; string string1 = ""; int numberVowels = 0; int numberNonVowels = 0; cout << "Enter string1: "; getline(cin, string1);...

  • c++, we have to write functions for the code given below and other instructions for it...

    c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...

  • #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating...

    #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating odd numbers, init to 0    int sumEven = 0; // For accumulating even numbers, init to 0    int upperbound; // Sum from 1 to this upperbound    // Prompt user for an upperbound    cout << "Enter the upperbound: ";    cin >> upperbound;    // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound    int number =...

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT