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...

  • in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call...

    in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call a function called makeArray to define an array of integers with the number of elements equal to the number of students surveyed. Call a function called getStudentData to allow the user to enter the number of hours each student spent watching Netflix into the array. Call a function called getAverage to calculate and display the average of the hours entered. Call a function called...

  • 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...

  • Create a program that creates an array of 500 random numbers between -10 and 10. Write...

    Create a program that creates an array of 500 random numbers between -10 and 10. Write several functions: Function 1 - prints the array with a certain number of elements per line. Prototype: void printArray(int array[], int numElements, int numPerLine, ostream &os) Function 2 prints only the array elements that are evenly divisible by an input number. Prototype: void printDivBy(int array[], int numElements, int numPerLine, int divBy, ostream &os) Function 3 takes the input array and returns the maximum, the...

  • // 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...

  • 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);...

  • 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...

  • 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 =...

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