Question

Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps....

Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps. This is a basic course and the furthest we have gone is BASIC arrays.

Write the code for the following problems using arrays. Write what would go in the “int main()” part of the program. For these problems, the entire program and program output is not needed. Just include what goes in the int main() part of the program.

1. Declare an integer array named stuff with 20 elements

2. Initialize each element of the array stuff to value 100.

3. Ask the user to enter each value of the array stuff (prompt and input)

4. Print all elements of the stuff array

5. Print all elements of the array stuff in reverse order

6. Sum all elements of the array stuff

7. Determine the average of all elements in the array stuff

8. Find the largest element in the array stuff

9. Subtract 1 from every element of the stuff array.

10. Divide all even-numbered elements in the stuff array (stuff[0],stuff[2],..stuff[20]) by 5, i.e. stuff[2] = stuff[2]/5

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

#include<iostream>
using namespace std;

int main ()
{
// 1. Declare an integer array named stuff with 20 elements
int stuff[20];

// 2. Initialize each element of the array stuff to value 100.
for(int i = 0; i<20; i++)
{
stuff[i] = 100;
}

// 3. Ask the user to enter each value of the array stuff (prompt and input)
for(int i = 0; i<20; i++)
{
cout<<"Enter element "<<i<<": ";
cin>>stuff[i];
}

//4. Print all elements of the stuff array
for(int i = 0; i<20; i++)
{
cout<<stuff[i]<<" ";
}
cout<<endl;
// 5. Print all elements of the array stuff in reverse order
for(int i = 19; i>=0; i--)
{
cout<<stuff[i]<<" ";
}
// 6. Sum all elements of the array stuff
int sum = 0;
for(int i = 0; i<20; i++)
{
sum+=stuff[i];
}

// 7. Determine the average of all elements in the array stuff
double average = (double)sum/20;

// 8. Find the largest element in the array stuff
int max = stuff[0];
for(int i = 1; i<20; i++)
{
if(max<stuff[i]){
max = stuff[i];
}
}

// 9. Subtract 1 from every element of the stuff array.
for(int i = 0; i<20; i++)
{
stuff[i] = stuff[i]-1;
}

// 10. Divide all even-numbered elements in the stuff array by 5
for(int i = 0; i<20; i+=2)
{
stuff[i] = stuff[i]/5;
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps....
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
  • USE C Programming LANGUAGE ONLY PLEASE. We use <stdio.h> if that helps. For the following problems,...

    USE C Programming LANGUAGE ONLY PLEASE. We use <stdio.h> if that helps. For the following problems, create the prototype function, calling function “int main()”, and function. So the function prototype, function call, and function header/body should be seen in the answer. The processor directive is not needed. Show a sample call from the int main() function for all problems. If you can for the first one please show proof that it has no errors. 1. Write a function called DisplayColumbiaUniversity...

  • Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at...

    Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at even subscripts 2. The sum of all elements at odd subscripts 3. The sum of all elements You are allowed to perform this functionality within main. Main program: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include <stdio.h> int main() { /* Declare array variables */ const int NUM_VALS = 4; int userValues[NUM_VALS]; int i; // counter int even = 0; int odd = 0; int sum = 0; /* Initialize...

  • Using C++ And #include <stdio.h> #include <stdlib.h> #include <time.h> a) Write a main function and declare...

    Using C++ And #include <stdio.h> #include <stdlib.h> #include <time.h> a) Write a main function and declare an array with 100 elements. b) In the main, initialize each number in the array to be a random number between 100 and 200. c) Write a function that returns the value of the smallest number in the array. Call this function from the main and print the result. d) Declare a 2D array with dimensions 2x2 and initilize the array, as follows: [1,...

  • C++. Write a program that copies the contents of one array into another array but in...

    C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.           - in main()                    - define 2 arrays of type int, 10 elements each                              - initialize one array with numbers 1 through 10                              - initialize all elements of the second array to 0                    - call function reverseCopy with both arrays as arguments                    - display the values of array number 2 (reversed order)           - reverseCopy...

  • C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop...

    C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1. (Notes) Note: These activities may test code with...

  • Please do in C and only use stdio.h. Write a program that reads n integers into...

    Please do in C and only use stdio.h. Write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. The values should be printed in ascending order. Turn in your code and input/result together. Suppose, for example, that you input the values -7 3 3 -7 5 5 3 as the elements of your array. Then your program should print -7...

  • Assignment: Write a program with each of the following methods. You can assume this program is...

    Assignment: Write a program with each of the following methods. You can assume this program is saved in a file called multiArrays.java. A sample main method and sample output is provided at the end of this section. All arrays created are of integer-type. Write a method to declare, initialize, and populate a two-dimensional array. Using the following header:                         public static int[ ][ ] declare2D(Scanner scnr) The user will indicate the size of the array. Write a method to declare,...

  • Write a C program to do the following...in this EXACT order: a. Declare an integer array...

    Write a C program to do the following...in this EXACT order: a. Declare an integer array named alpha of 50 elements. b. Use a for loopt to initialize each element of alpha to its index value (e.g. alpha[0] = 0, alpha[1]=1, etc.). c. Output the value of the first element of the array alpha. d. Output the value of the last element of alpha. e. Set the value of the 25th element of the array alpha to 62 and output...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

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