1 Replies - 1631 Views - Last Post: 24 April 2007 - 03:31 PM Rate Topic: -----

#1 jamescr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-April 07

2d Arrays function calling

Posted 24 April 2007 - 01:32 PM

I am having a difficult time with this project. What i need to do is make a menu to manipulate grades. I cant seem to get my function to accept the 2d array. I want to output what my input file looks like but i cant get the information from my main program to my functions. Here is my code.

 #include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;

const int nr=8;
const int nc=6;

void fun1(int[nr][nc]);
void fun2(int[nr][nc]);
void fun3(int[nr][nc]);
void fun4(int[nr][nc]);
void fun5(int[nr][nc]);
void fun6(int[nr][nc]);//prototypes


ifstream file("grades.dat");//input  
ofstream out ("gradesout.dat");//output
int main () 
{
int idnum[nr][nc], lab1[nr][nc], lab2[nr][nc], lab3[nr][nc], lab4[nr][nc], exam[nr][nc];
int des;
int i, j; 

int ar1[nr][nc];
if (file.is_open()) 
{  
while(!file.eof())

for(i=0; i<6; i++)
{
for(j=0; j<nc; j++)
{
file>>idnum[i][j]>> lab1[i][j]>> lab2[i][j]>> lab3[i][j]>>lab4[i][j]>>exam[i][j];

cout<<idnum[i][j]<<lab1[i][j]<<lab2[i][j]<<lab3[i][j]<<lab4[i][j]<<exam[i][j];	//for testing
}}
cout<<"								Mid-term Grading Program\n\n\n"<<endl;
cout<<"1-Print out the Grades for the students"<<endl;
cout<<"2-Calculate and print the lab grade average and mid term grade for each student"<<endl;
cout<<"3-Calculate and print the average grade of all students for each lab and exam"<<endl;
cout<<"4-Sort and print the ids and grades in order by ascending id numbers"<<endl;
cout<<"5-Sort and print the ids and grades in the order of descending grades"<<endl;
cout<<"6-Change the grade for a student"<<endl;
cout<<"7-End the Program"<<endl;
cout<<"\nMake your Selection: ";
cin>>des;

if((des>7) || (des<0))
{cout<<"That is not a valid number try agian: "<<endl;

if(des==1)
{
fun1(ar1);
}

if(des==2)
{cout<<"2";}

if(des==3)
{cout<<"3";}

if(des==4)
{cout<<"4";}

if(des==5)
{cout<<"5";}

if(des==6)
{cout<<"6";}

if(des==7)
{return 0;}
}
}//end while 
return 0;//end program
}
//******************************************************************

int fun1(int[][1])
{
int i,j;
{

for(i=0; i<nr; i++)
for(j=0; j<nc; j++)
out<<setw(5)<<ar1[i][j];
return (1);
}
}


This post has been edited by jamescr: 24 April 2007 - 01:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: 2d Arrays function calling

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: 2d Arrays function calling

Posted 24 April 2007 - 03:31 PM

Here is your program so that it compiles, but I don't think it does what you want. Mostly because you didn't use brackets { } properly. Also you need to define a function in the same way that you decalre it. Not only that, but when you declare a function you are not required to name arguments, but when you define the function you MUST give an identifier to each vaiable.

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;

const int nr=8;
const int nc=6;

void fun1(int[nr][nc]);
void fun2(int[nr][nc]);
void fun3(int[nr][nc]);
void fun4(int[nr][nc]);
void fun5(int[nr][nc]);
void fun6(int[nr][nc]);//prototypes


ifstream file("grades.dat");//input  
ofstream out ("gradesout.dat");//output
int main ()
{
	int idnum[nr][nc], lab1[nr][nc], lab2[nr][nc], lab3[nr][nc], lab4[nr][nc], exam[nr][nc];
	int des;
	int i, j;

	int ar1[nr][nc];
	if (file.is_open())
	{  
		while(!file.eof())
		{
			for(i=0; i<6; i++)
			{
				for(j=0; j<nc; j++)
				{
					file>>idnum[i][j]>> lab1[i][j]>> lab2[i][j]>> lab3[i][j]>>lab4[i][j]>>exam[i][j];

					cout<<idnum[i][j]<<lab1[i][j]<<lab2[i][j]<<lab3[i][j]<<lab4[i][j]<<exam[i][j];	//for testing
				}
			}
		}
		cout<<"								Mid-term Grading Program\n\n\n"<<endl;
		cout<<"1-Print out the Grades for the students"<<endl;
		cout<<"2-Calculate and print the lab grade average and mid term grade for each student"<<endl;
		cout<<"3-Calculate and print the average grade of all students for each lab and exam"<<endl;
		cout<<"4-Sort and print the ids and grades in order by ascending id numbers"<<endl;
		cout<<"5-Sort and print the ids and grades in the order of descending grades"<<endl;
		cout<<"6-Change the grade for a student"<<endl;
		cout<<"7-End the Program"<<endl;
		cout<<"\nMake your Selection: ";
		cin>>des;

		if((des>7) || (des<0))
		{
			cout<<"That is not a valid number try agian: "<<endl;

			if(des==1)
			{
				fun1(ar1);
			}

			if(des==2)
			{cout<<"2";}

			if(des==3)
			{cout<<"3";}

			if(des==4)
			{cout<<"4";}

			if(des==5)
			{cout<<"5";}

			if(des==6)
			{cout<<"6";}

			if(des==7)
			{return 0;}
		}
	}//end if
	return 0;//end program
}
//******************************************************************

void fun1(int ar[nr][nc])
{
	int i,j;


	for(i=0; i<nr; i++)
	for(j=0; j<nc; j++)
	out<<setw(5)<<ar[i][j];
	return;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1