Please help

below is my simple cpp program. it does not print the value of k as float. it's printing the integer value.

#include<iostream>
using namespace std;
int N,sum,size;
float ave(int,int);
int main()
{
cout<<"Enter the number of the elements in the array:";
cin>>N;
int a[N],
sum=0;
for (int i = 0; i < N; i++)
{
cout<<"Enter the "<<i+1<<"th element of the array.";
cin>>a[i];
sum+=a[i];
}
cout<<"the sum is: "<<sum;
size=sizeof(a)/sizeof(int);
cout<<"the size is:"<<size;
float k=sum/size;
cout<<k;
}
k is the result of 2 integers divided. you should cast one to force it to do floating point division:
eg: float k = sum/(double)size;


int a[N] is technically not allowed; the size of an array must be a constant value (not entered by the user, but set in the code eg const int N = 42; ). Most compilers allow it if you are not using strict language, but you want to avoid this or at least remember that its not a good thing to do.
there is an advanced array type called vector that can be sized to fit:
#include <vector>
...
cin >> N
vector<int> a(N); //this is OK to do.
a[0] = 0; //use just like an array

if your professor frowns upon doing things you have not seen yet, just take a note of it for later.

welcome to the forum... please use code tags in the future, its easier to read.
Last edited on
@sarojbsubedi - in your code, why is size not just N ?

Consider:

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
include <vector>
#include <iostream>

int main()
{
	size_t N {};
	double sum {};

	std::cout << "Enter the number of the elements in the array:";
	std::cin >> N;

	//int a[N] {};
	std::vector<double> a(N);

	for (size_t i = 0; i < N; ++i) {
		std::cout << "Enter the " << i + 1 << "th element of the array: ";
		std::cin >> a[i];
		sum += a[i];
	}

	std::cout << "the sum is " << sum << '\n';
	std::cout << "the size is " << N << '\n';
	const auto k {sum / N};
	std::cout << "the average is " << k << '\n';
}


Hello sarojbsubedi,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

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

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



I feel that understanding what is wrong with your posted code is more helpful than just how to change it.

For the beginning:
1
2
3
4
5
6
7
8
9
10
#include<iostream>

using namespace std;  // <--- Best not to use.

int N, sum, size;

float ave(int, int);  // <--- Never defined or used.

int main()
{

First some blank lines make the code easier to read.

Prefer to use "double" over "float".

Avoid global variables, e.g., "N", "sum" and "size". These are better defined in "main".

As a global variable "size" is a problem. As a global variable it is not a problem, but when you try to use it in "main" it is a problem.

In "main" size=sizeof(a)/sizeof(int); "size" is a problem because you told the compiler to use "std::size", because of line 3, and "std::size" is a function that is expecting 1 or more parameters. Which you do not give here.

A good variable name can be very helpful in your code. It makes it much easier to follow.

I do not think you are up to "vectors" yet , so here is an example of reworking your code. Notice the blank lines and variable names. The variable names are a suggestion. You are free to change anything that you want, just keep in mind how the name should describe what the variable is being used for.

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
44
#include <iostream>
#include <iomanip>  // <--- For setprecision.
#include <string>

using namespace std;  // <--- Best not to use.

//double ave(int, int);  // <--- Never defined or used.

int main()
{
    constexpr int MAXSIZE{ 20 };
    const std::string abrvs[]{ "st", "nd", "rd", "th", "th" };

    int sizeUsed{}, arrSize{};  // <--- ALWAYS initialize all your variables.
    double sum{};
    double numArray[MAXSIZE]{};

    cout << "\n Enter the number of the elements in the array: ";
    cin >> sizeUsed;
   
    std::cout << std::fixed << std::showpoint << std::setprecision(2) << '\n';
    
    for (int idx = 0; idx < sizeUsed; idx++)
    {
        cout << " Enter the " << idx + 1 << abrvs[idx] << " element of the array. ";
        cin >> numArray[idx];

        sum += numArray[idx];
    }

    cout << "\n the sum is: " << sum;

    arrSize = sizeof(numArray) / sizeof(double);  // <--- This is the size of the whole array.

    cout <<
        "\n The size is: " << arrSize << " elements.\n"
        " Only " << sizeUsed << " elements are used.\n";

    double avg = sum / sizeUsed;

    cout << " The average is: " << avg;

    return 0;  // <--- Not required, but makes a good break point for testing.
}

This is written for a minimum C++11 standards and what is available from it.

The header file "string" and line 12 is optional. Just to give you an idea of something that you could do and there is probably a better way to do this.

As you look through you will see how I used the new line, (\n), so that the output is not all run together and hard to read.

See what you think. Any questions let me know.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

int main()
{
   int N;
   cout << "Enter the number of elements in the array: ";
   cin >> N;
   int *a = new int[N];
   int sum = 0;
   for ( int i = 0; i < N; i++ )
   {
      cout << "Enter element " << i + 1 << ": ";
      cin >> a[i];
      sum += a[i];
   }
   cout << "Sum: " << sum << '\n'
        << "Size: " << N << '\n'
        << "Average: " << ( sum + 0.0 ) / N << '\n';
   delete [] a;
}


Of course, if this is all you are going to do then you do not actually require an array.
Last edited on
Topic archived. No new replies allowed.