(Question) Write a C++ program that includes the following:
Declare an integer array of size 10
Ask user to enter 10 numbers for the array
Print out the array in index ascending order
Print out the array in index descending order
Find the sum of the array and print it out
(what I have Tried , just confused how to ascending and descending part)
#include <iostream>
using namespace std;
int main(){
int a[10];
cout << " Please give me 10 numbers: " << endl;
for (int i=0; i<10;i++)
Why are you setting a[i] to i and then overwriting this value with that entered bythe user? I suggest you make sure you understand the replies and code to your previous post before proceeding.
#include <iostream>
usingnamespace std;
int main()
{
int a[10];
cout << " Please give me 10 numbers: " << endl;
for (int i = 0; i < 10; i++)
{
a[i] = i; // not needed, you get input with the next line
cin >> a[i];
}
for (int i = 0; i < 10; i++)
{
if ()
}
return 0;
}
You've done a number of the assignment steps already:
Sunnycoder wrote:
Declare an integer array of size 10
Did that at line 7.
Ask user to enter 10 numbers for the array
Lines 9-15. You don't need line 13.
Print out the array in index ascending order
Lines 17-21 are a good start, your for loop can be used to properly index your array for output. Instead of the if statement you could use std::cout: std::cout << a[i] << '\n';
This will display the array in a vertical format. If you want the array displayed in a horizontal format:
17 18 19 20 21
for (int i { }; i < 10; i++)
{
std::cout << a[i] << ' ';
}
std::cout << '\n';
Print out the array in index descending order
If a for loop going from 0 to 9 is ascending indexing, what would be descending indexing in a for loop? for (int i = 9; i >= 0; --i)
Figuring out how to sum the array could use a for loop, I'll leave that to you.
You are not zero initializing your add variable at line 7. It can start out with a random value. And/or your compiler will flag a warning about not initializing it. int add { };
Your 2 for loops at lines 24 & 29 will make your array indexing go out of bounds. You want i < 10 in both.
Your output for loops will print all the numbers together on one line without any spaces. Is that really what you want?
Your summation for loop (line 26) is not correct. add = add + a[i]; OR add += a[i];
DO NOT ask programming questions by private message. Any questions you have need to be asked publicly, such as here in this topic. Questions and related answers might help others in the future by reading this topic.
#include <iostream>
int main()
{
// create a constant variable to use when creating regular arrays...
// that avoids using "magic numbers", allowing you to change the size
// of an array in one place.
constint num_array { 10 };
int arr[num_array];
std::cout << "Enter " << num_array << " numbers:\n";
for (int i { }; i < num_array; i++)
{
std::cin >> arr[i];
}
std::cout << "\nArray forward:\n";
for (int i { }; i < num_array; i++)
{
std::cout << arr[i] << ' ';
}
std::cout << "\n\nArray reversed:\n";
for (int i { num_array - 1 }; i >= 0; i--)
{
std::cout << arr[i] << ' ';
}
std::cout << "\n\n";
int sum { };
for (int i { }; i < num_array; i++)
{
// summing the array elements
// https://www.learncpp.com/cpp-tutorial/arithmetic-operators/
// (arithmetic assignment)
sum += arr[i];
}
std::cout << "The array sum: " << sum << '\n';
}