954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Organizing the code....

I have a problem to solve, and I came so close with algorithm, but I am having hard time organizing them so they work.

I will post the question and the codes I have.

Can someone help me organizing them?


The program is expected to read input information from a textfile called “grade.txt” and write its output to the terminal.

The format of the input file is: First line will have two integer values, as x and y.

For example, 3 and 4. First number, 3, indicates the number of the students. Second number, 4, specifies the number of homework. There will be x rows, each with y columns (3 rows and 4 columns) where each column will hold a grade between 0-100. Below, you can see the contents of a possible input file:

3 3
92 90 70
80 90 20
90 70 88

Your program is to calculate the average of any number of grades (expect up to 10 grades in each row). Include at least 3 functions:
• main, which declares the a two-dimensional grades array (10 by 10), calls the other methods, and writes output of the grades, their average and a letter grade ( >= 90 is A, >= 80 is B, >=70 is C, >= 60 is D, and E otherwise).
• get_grades, which gets input of integer grades from a file to the 2-dimensional array.
• calculate_average, which uses the grades array and passes the average back to main.

#include <iostream>
#include <fstream> 
using namespace std;

int get_grades()							// GET GRADES // 
{
	int student;
	int hw;
	int gradebook = [10][10];
	in_stream >> student;
	in_stream >> hw;
	ifstream OpenFile;				// ifstream --> input
	OpenFile.open("grade.txt");		//	Open file grade.txt
	char ch;
	while (OpenFile)
	{
		OpenFile.get(ch);
		cout << ch;
	}

	for (int i=0; i< student; i++)
		for (int j=0; j<hw ; j++)
			in_stream >> gradebook [i][j];
}


 


int calculate_average (main)				// calculate average //
{
	int Avg (int[],int)
		int total = 0;
	for (int k=0; k<Avg; k++)
		total += student_1[k];
			return (total/hw)
}


int main ()									// MAIN //
{
	int student_1[10];
	for (int i=0; i<student_1; i++)
		cout << "student # " << i << "\t";
		
		{
		for (int j=0; j<hw ; j++)
			cout << gradebook [i][j] << "\t";
				student_1[j] = gradebook [i][j];
		}

	int avg (student_1[], hw);

int  Avg = total / 100;
 char grade;
 switch (temp)
    {
     case 100 : grade = 'A Plus!';
               break;
     case  90 : grade = 'A';
               break;
     case  80 : grade = 'B';
               break;
     case  70 : grade = 'C';
               break;
     case  60 : grade = 'D';
               break;
     default : grade = 'F';
    } // switch

	cout << endl;
}
jdh1231
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

always have a return statement at int main to properly stop the execution and also at the non void functions

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

hm. ok. is there anywhere that you think I should get rid of or fix?

jdh1231
Newbie Poster
8 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

I'm not yet adept at C++,I just know the fundamentals, so I think you need to wait for veteran members to have a look at it :)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

OK, you posted your assignment. You posted your code. But you didn't explain exactly what you want help with. "Organize" is not a specific enough question. Although proper formatting would be a really good start. It helps you. It helps us. And your instructor won't have to work so hard checking the code so it helps him, too.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Hi There

I agree with Walt 100% and I would just like to add the following:

Read your assignment very clearly and Identify the requirements,

1 thing I see is that your assignment requires you to create the 2-dimensional array in your main()... I'll leave the rest

1 other thing, and this is not a requirement at all, but I found it helps me a lot to add as many as possible comments to my code to not only explain it to others when I need help, but to enable me to read the flow of my program easier,

That way proper formatting becomes evident,

Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

The first opinion you should get is that of your compiler.
This code will not compile.

Organizing it is important, but not quite as important as making it functional (and following the actual instructions).

Step through it mentally and think about WHEN things are to happen.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 
Organizing it is important, but not quite as important as making it functional (and following the actual instructions).


I completely disagree.
A good organization is best done when designing and writing the code -- long before it's functional. This makes making it functional easier.

Organizing after it's functional simply means
1) it takes longer to get it functional
2) alot of work getting the mess organized after it's functional
3) fixing the code after organization has broken it.
IOW - it takes 3-4 times longer to create a single program.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

After a while, a developer can imagine "chunks-of-functionality" and immediately put the code into that arrangement -- and for the long term serves a greater purpose.

...but that comes with experience.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You