Program reads "error"

I was told to create a program for my intro to C++ class. I plug the program into Microsoft Visual Studio Express 2015 and it will not read it.
Can someone aid me in fixing this program so that it will read?
Also, here are my giving instructions for additional understanding:
---------------------------------------------------------------------------
Please submit:
1. Flowchart of your program. (4 points)
2. Printout of your C++ program with a heading comment (Do not forget to
add comments within your program). (4 points)
3. Copy of a screenshot after your program is executed. (2 points)
4. You must use a 2 dimensional array in order to receive any credit.
/*
ELEN 1301-48F Programming Assignment #11.
Name : Your name.
Student ID : Your student ID #.
Due date :
Purpose of the program:
Create a program that generates random numbers, store them in a 10 X
10 array, and find the average for each row, column, as well as whole
numbers.
Section 1 Set a random number seed with 2 followed by the last 3
digits of your student ID.
Section 2 Generate random numbers, each ranging from 0 to 9, and store
them in a 10 X 10 array.
Section 3 Calculate the average for each row, column and whole.
Section 4 Show the result to the screen. Averages must show 2 digits
below the decimal point.
-------------------------------------------------------------------
#include <iostream>
#include<time.h>
#include<iomanip>
using namespace std;
const int size=10;
int main()
{
int a[10][10];
double total,avg,tsum;
srand(2);
for(int i=0;i<size;i++)
{
total=0;avg=0.0;
for(int j=0;j<size;j++)
{
a[i][j]=rand()%10;
cout<<a[i][j]<<"\t";
total+=a[i][j];
tsum+=a[i][j];
}
avg=total/3;
cout<<fixed<<setprecision(2)<<avg<<endl;
}
int j=0;
while(j<size)
{
total=0;avg=0;
for(int i=0;i<size;i++)
{
total+=a[i][j];
}
avg=total/size;
cout<<fixed<<setprecision(2)<<avg<<"\t";
j++;
}
cout<<tsum/size*size;
cout<<endl;
return 0;
}
You have a namespace collision with your variable size. When you use using namespace std; the compiler will grab all functions (which is A LOT OF FUNCTIONS) from the std namespace and assume that is what you are trying to call if there is no identifier within the local scope. There are a few ways to fix this:
1) Don't use using namespace std;, instead grab each function from std that you will use explicitly, like so:
1
2
3
4
using std::cout;
using std::fixed;
using std::setprecision;
using std::endl;

This is also good practice in general, especially with larger programs or collaborative projects.

2) Move const int size = 10; into the main functions scope

3) Rename size

I would recommend doing both one and two, but doing any of the three should fix your problem.
Sorry im pretty new at programming. Is this okay? i did what you said edge6768 except for the 2nd one. Im not sure what you mean by move it into the main functions scope. Thank you much anways!

#include <iostream>
#include<time.h>
#include<iomanip>
using std::cout;
using std::fixed;
using std::setprecision;
using std::endl;
const int mark = 10;
int main()
{
int a[10][10];
double total, avg, tsum;
srand(2);
for (int i = 0; i<mark; i++)
{
total = 0; avg = 0.0;
for (int j = 0; j<mark; j++)
{
a[i][j] = rand() % 10;
cout << a[i][j] << "\t";
total += a[i][j];
tsum += a[i][j];
}
avg = total / 3;
cout << fixed << setprecision(2) << avg << endl;
}
int j = 0;
while (j<mark)
{
total = 0; avg = 0;
for (int i = 0; i<mark; i++)
{
total += a[i][j];
}
avg = total / mark;
cout << fixed << setprecision(2) << avg << "\t";
j++;
}
cout << tsum / mark*mark;
cout << endl;
return 0;
}
Put simply, "scope" is what is visible to the program at a specific poiint in the code.

For example:
1
2
3
4
5
6
7
8
9
10
11
int main() {
for(int i = 0; i < 100; i++) {
// i is visible here (or "in scope") and can be used within the brackets
i = 10; // This is valid
}

// i is NOT visible here (or "out of scope") and cannot be used outside the brackets
i = 20; // This would cause an error

return 0;
}


So, to put const int mark = 10; inside of the main functions scope, just move it after main begins. Your program should compile as is, but global variables (or variables not within a set of { } brackets) are not always a good idea.
I managed to fix that part now. The only error that i have is that "uninitialized local variable
'tsum' used".
So you should initialise it, before attempting to use it.

tsum += a[i][j];

What value do you think tsum will have, on the first pass through your loop?

Also, please use code tags when posting code, to make it readable (as you were already asked to do in your last thread):

http://www.cplusplus.com/articles/z13hAqkS/
Ok sorry! i was wondering how you guys did that. between which lines should i insert tsum += a[i][j]; ? Is this all i need to do to make it work?

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
#include <iostream>
#include<iomanip>
#include <cstdlib> /* srand, rand */
#include <ctime> // time

using namespace std;
const int mark = 10;

int main() {
	int a[mark][mark];
	double total, avg, tsum;
	srand(2);
	for (int i = 0; i<mark; i++) {
		total = 0; avg = 0.0;
		for (int j = 0; j<mark; j++) {
			a[i][j] = rand() % 10;
			cout << a[i][j] << "\t";
			total += a[i][j];
			tsum += a[i][j];
		}
		avg = total / mark;
		cout << fixed << setprecision(2) << avg << endl;
	}
	int j = 0;
	while (j<mark) {
		total = 0; avg = 0;
		for (int i = 0; i<mark; i++) {
			total += a[i][j];
		}
		avg = total / mark;
		cout << fixed << setprecision(2) << avg << "\t";
		j++;
	}
	cout << tsum / mark*mark;
	cout << endl;
	return 0;
}

It has nothing to do with where you insert that line. As I already said, you need to initialise the value of the variable.

What value do you think tsum has, before the first time you increment it?
Last edited on
got it! appreciate the help guys!
Topic archived. No new replies allowed.