Hi ! I'm new to programing and I'm learning on my own so it's quite hard for me to do tasks without any examples . Can someone help me with a task ? There's an array stud(m,n) in which m is students and n is grades . create a new array avg(m) which is made from students grade average . It is told to use a 2d array. This is some code i wrote but it only shows what did the user input.
#include <iostream>
usingnamespace std;
int main(void)
{
int m,n ;
int myArray[10][10]={n,m};
cout<<"how many students is there ? ";
cin>>n;
cout<<"how many classes does he have ? ";
cin>>m;
// myArray[width][height];
for (int i = 0; i < n; ++i)
{ cout<<i+1<<"-ist student "<<"\n";
for (int j = 0; j < m; ++j)
{
cout<<(j+1)+n*i<<": grade ";
cin>>myArray[i][j];
}
}
int masyvas [10][10];
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
masyvas[i][j]=myArray[i][j];
}
}
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
cout<<masyvas[i][j]<<" ";
}
cout <<endl;
}
}
Your first array is an array of up to 10 students with up to 10 test grades. So now you want to compute the average test grade for each student and store these averages in a single dimensional array, one element for each student.
By the way on line 8 there is no need for the initialization, especially since both m and n are not initialized.
Also you should be checking that the user of your program enters 10 or less for each of these variables, since your array is set to 10.
#include <iostream>
usingnamespace std;
int main(void)
{
int m,n,a,b,r,c ;
double total;
int myArray[10][10]={n,m};
int vidurkis[10];
double average =0.00;
a=0;
cout<<"how many students is there ? ";
cin>>n;
cout<<"how many classes does he have ? ";
cin>>m;
for (int i = 0; i < n; ++i)
{ cout<<i+1<<" student "<<"\n";
for (int j = 0; j < m; ++j)
{
cout<<(j+1)+n*i<<": grade ";
cin>>myArray[i][j];
for (int x=0;x<j;x++){
}
}
}
for (int r=0;r<n;r++){
cout<<"student "<<r+1<<" ";
for (int c=0;c<m;c++){
cout<<myArray[r][c]<<" ";
total+=myArray[r][c];
}
average=total/m;
cout<<"Average: "<<average<<endl;
total=0;
average=0.00;
}
return 0;
}