Array question

(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++)

{
a[i]=i;
cin >> a[i];
}

for (int i=0; i<10;i++){

if ( )
}



return 0;
}[/code]
Are you reading replies?
https://www.cplusplus.com/forum/general/277264/#msg1196714

Are you learning anything?

Or just dumping assignments one after another hoping for manna from heaven?
To sort an array, use std::sort() http://www.cplusplus.com/reference/algorithm/sort/

1
2
a[i]=i;
cin >> a[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.
oh sorry guys new hear and just learning stuff , I have tried what you guys are saying but I am still confused!
What are you confused about?
Post what you have (with proper code tags) and state clearly what you're confused about.
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

You were almost there, you have the closing code tag, the opening code tag is MIA.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace 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.
ok I did this and still not working !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;
int main()
{
    int a[10];
    int add=0;
     
    
    cout << "Enter the numbers:" << endl;

    for (int i = 0; i <= 9; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i <= 9; i++)
    {
        cout << a[i];
    }
    for (int  i=9; i>=0;i--)
    {
      cout << a[i];
    }

    for (int i=0; i < 10; i++)
    {
      add = a[i]+a[i]; 
      cout << add;
    }
    return 0; 

}
I did this and still not working !
You need to explain in more detail what results you expect to get, and what results you are getting.

I have an an idea what you want, your attempt at summing the array in your last for loop is borked, bad.
25
26
27
28
29
30
    for (int i=0; i < 10; i++)
    {
        add = add + a[i];
        // OR add += a[i];  // I prefer this
    }
    cout << add;

One possible way to do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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.
   const int 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';
}
Enter 10 numbers:
2 4 6 8 10 12 14 16 18 20

Array forward:
2 4 6 8 10 12 14 16 18 20

Array reversed:
20 18 16 14 12 10 8 6 4 2

The array sum: 110
oh wow thank you sire , kinda fiqured I was messing up in the int part
Topic archived. No new replies allowed.