One Demensional Array

hey guys im new in programming i just need help how to make a program that can calculate sum and average if we input five values...?
thanks this is for school project :)

Please note, that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
well this is the code i am making ..

#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
int main()
{
   int n, count;
   float x, sum, avg;
 
   sum = 0;
   cout << "How many numbers?  ";
   cin >> n;
   int size= n;
   int array[size];
   for (count=1; count<=n; count++){
       cout << "Enter Number:  ";
         cin >> array[n];
        sum = sum + array[n];
   }  //end for
   cout << "The sum is " << sum << endl;
   avg = sum / n;
   cout << "The average is  " << avg << endl;
   system("pause>0");
   }

can you try it .. i have a problem. there
Last edited on
First, use of code tags helps discussion. See http://www.cplusplus.com/articles/jEywvCM9/

Second, "a problem there" does not tell what kind of errors we should look for. You should learn to tell the details.

1
2
3
cin >> n;
int size= n;
int array[size];

VLA. Variable length array. The compiler does not know how large the array should be, because user decides it during runtime. C++ standard does not support VLAs. There are ways to create runtime array, dynamically.
1
2
3
4
cin >> n;
if ( n < 1 ) n = 1; // failsafe
int size= n;
std::vector<int> array(size);


1
2
3
4
for (count=1; count<=n; count++)
{
  cin >> array[n];
  sum = sum + array[n];

The array has n elements. The index of first element is 0. The index of last element is n-1.
Trying to access element with index n is an out of range error.
Therefore, for ( count=0; count < n; ++count )

On first iteration you should use the first element, array[0]. Alas, you use the (non-existent) array[n]. Value of count is 0 on first iteration and therefore array[count] is good.

You have integer numbers. It would make sense to have integral sum too.
One can force a floating point division with a cast:
avg = static_cast<float>(sum) / n;
Can we? Yes. Will we? No.

We can look at the code and see the issues.

float x x is declared but never actually used.

1
2
3
   cin >> n;
   int size= n;
   int array[size];
This is undefined. A static array must have the size by compile time not run time. To do it your way you will need to use the operators new [] and delete [];. Something like
1
2
3
std::size_t size = 0;
cin >> size;
int *array = new int[size];
Also, why did you use variable n and size for the size instead of just one?

1
2
3
4
5
   for (count=1; count<=n; count++){
       cout << "Enter Number:  ";
         cin >> array[n];
        sum = sum + array[n];
   }  //end for 
arrays start at index 0 not 1. Think of it as an offset from the first element's address. So the first one would have an offset of 0. This also means that the last element will be size - 1. Anyways you should be using count not n for the index.

sum = sum + array[n]; What will it be the first time around? sum is never initialized to a value (should be 0) when you declare it on line 5.

May I ask why you are using an array anyways? You could have simply done something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    std::size_t size = 0;
    std::cout << "Please enter the amount of numbers to be entered: ";
    std::cin >> size;
    
    std::size_t sum = 0;
    std::size_t input = 0;
    for(std::size_t i = 0; i < size; ++i)
    {
        std::cout << "Please enter number " << i + 1 << ": ";
        std::cin >> input;
        sum += input;
    }
    
    std::cout << "Sum: " << sum << std::endl;
    std::cout << "Avg: " << sum / static_cast<double>(size) << std::endl; //(double)size
    
    return 0;
}
Please enter the amount of numbers to be entered: 3
Please enter number 1: 1
Please enter number 2: 2
Please enter number 3: 4
Sum: 7
Avg: 2.33333


*Edit beat by [b]keskiverto and fixed a typo
Last edited on
tnx keskiverto and giblit.. i learn something new:) i already edited my code thank you for the help..

and by the way
1
2
3
std::cout << "Please enter number " << i + 1 << ": ";
        std::cin >> input;
        sum += input;

this is the code that i wanted hehe tnx tnx tnx !!

our professor said that make it in 1d array :) :)
Last edited on
our professor said that make it in 1d array :) :)

The "simplest" solution would be the vector: std::vector<int> array(size);
Sadly, courses seem to call it "advanced".

Explicit dynamic allocation is next: int *array = new int [size];
It expects appropriate deallocation. Again, there is a "simple" solution: std::unique_ptr<int[]> array( new int [size] );[/code]
Which again seems too "advanced".


What is left is sensible default.
1
2
3
4
5
6
7
const std::size_t Limit = 256;
int array[ Limit ];
...
std::cin >> size;
if ( Limit < size ) {
  // Report the error. Exit or shrink size to Limit
}

Now we have a "simple static 1D array". Its size is fixed.
However, the program cannot have more than Limit elements in the array.
Furthermore, if typical program run has size much less than Limit, then much more memory is reserved than required.
It is up to you to decide what is sensible; 256 is way more than 5.


We already know that the sum can be computed without an array. Courses, however, do not focus on simple, safe, and clever, but on practice, practice, and practice (of something). Therefore, a laborious student would not write one, but two loop; first loop to read values into array and second loop to calculate sum from array. It demonstrates the principle of dividing the task into simple, one-purpose subtasks.
Topic archived. No new replies allowed.