question on arrays

hey guys, i was trying to make this simple program using arrays.. we are supplied with a text file called data.txt.. in the same directory as the program.. the information inside data.txt is for example 2 columns:

Code:
3
12 13
14 15
13 67

i have learnt that the first line indicates how many rows of data is there.. we are supposed to store each column into seperate arrays ( single linear arrays) and then use those arrays to do functions like finding the max number in column 1 etc..

i have written this part of code

Code:
#include <iostream.h>
#include <stdlib.h>
#include <fstream>
int main()
{
ifstream file1;
int numRecords = 0;
int col1[10];
int col2[10];
file1.open("data.txt");
file1>>numRecords;
for(int i = 0;i<numRecords;i++ )
{
file1>>col1[i]>>col2[i];
}

but now i am confused that for example if i want to find out the maximum number in column1... what should be my arguments .. i have to use user defined functions..

if i write

int findMax (..... , ......., .....)
what should i be entering thereWacko or if i just use a simple 'for' loop to check each and every number in 'col1' array

my statemnt would be like for (int i = 0; i < .......; i++)

i mean how will u make it determine the size which is already stated in the text file.. which in this case is 3..

any help would be appreciated.
A nice simple way would be to just make a function that takes in both an element of col1 and col2. And use it inside a for loop in your main.

EDIT : Remember though your col1 and col2 can only hold a max of 10 values. So don't make your text file bigger than 10 lots of number to compare. If you do want more increase the size of your col1 and col2.

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
45
46
#include <iostream>
#include <stdlib.h>
#include <fstream>

void MinMax(int colOne, int colTwo)
{
	if( colOne > colTwo )
	{
		std::cout << "[col1] " << colOne << " Is greater than [col2] " << colTwo << std::endl; 
	}
	else if( colTwo > colOne )
	{
		std::cout << "[col2] " << colTwo << " Is greater than [col1] " << colOne << std::endl; 
	}
	else if( colOne == colTwo )
	{
		std::cout << "[col1] " << colOne << " Is equal to [col2] " << colTwo << std::endl; 
	}
}

int main()
{
	std::ifstream file1;
	int numRecords = 0;
	int col1[10];
	int col2[10];

	file1.open("data.txt");
	file1 >> numRecords;

	for(int i = 0; i < numRecords; i++ )
	{
		file1>>col1[i]>>col2[i];
	}
	std::cout << "Number of records found : " << numRecords << std::endl;
	for(int i = 0; i < numRecords; i++ )
	{
		std::cout << "col1[" << i << "]  = " << col1[i] << "\tcol2[" << i << "]  = " << col2[i] << std::endl;
	}
	std::cout << std::endl;

	for(int i = 0; i < numRecords; i++ )
	{
		MinMax(col1[i], col2[i]);
	}
}
Last edited on
thanks a lot for the reply.. i really understood the user defined function concept now.. i have another question though if you could please help.

my data.txt now looks like
1
2
3
4
5
6
30 50 10 60
4
S11111111 30 50 10 60
S22222222 0 0 0 0
S33333333 15 10 5 30
S44444444 18 35 9 22


The first column contains the student id.

The second column contains the mid-semester exam mark

The third column contains the Project Mark

The forth column contains the Quiz mark

The fifth column contains the final exam mark

The first row contains the maximum attainable mark for each assessment (Mid-test mark, Project
mark, Quiz mark and Final exam mark) respectively

The second row of the data file contains the number of student records in the file. For instance, in this case, 4 will mean that there are four students in the class and four student details in the data file.

the formula for calculating total is given as

Tot = Mid-test / Mid-test_max * 15 + Project / Project_max * 20 +
Quiz / Quiz_max * 5 + Final_exam / Final_exam_max * 60

the max mark for each test can be edited by the lecturer at any time. therefore it is not constant.

my question is that for eg. i know 4 indicates the amount of rows of data.. but while calculating the total how to make ur array know that the first row ONLY is the max amount of marks for each test.. as it falls under the same column as the id no. , mid test mark, quiz mark etc.. it gets mixed up..
Last edited on
I tried to do it how you were laying things out. This isn't everything it just reads in all the data according to how you were trying to do it in your first post.

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
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>

int main()
{
	std::ifstream file1;
	int maxMidExamMark, maxProjectMark, maxQuizMark, maxFinalExamMark;
	int numRecords = 0;

	file1.open("data.txt");
	file1 >> maxMidExamMark >> maxProjectMark >> maxQuizMark >> maxFinalExamMark;
	file1 >> numRecords;

	// This is an ugly way of doing something like this but for the sakes of
	// you probably haven't coverd structs and what not.
	// I'd advice you go read over - http://cplusplus.com/doc/tutorial/pointers/
	// for more info.
	const int MAX_ID_SIZE = 10;
	char** studentId       = new char*[numRecords];
	for( int i = 0; i < numRecords; i++ )
		studentId[i] = new char[MAX_ID_SIZE];
	//if you dont like the 4 above lines remove them and use "std::string* studentId = new std::string[numRecords];"

	int* midExamMark       = new int[numRecords];
	int* projectMark       = new int[numRecords];
	int* quizMark          = new int[numRecords];
	int* finalExamMark     = new int[numRecords];

	//Check everything loaded in correctly
	std::cout << "--- Student Evaluation Program ---" << std::endl;
	std::cout << "Max Mid Exam Mark : " << maxMidExamMark << std::endl;
	std::cout << "Max Project Mark : " << maxProjectMark << std::endl;
	std::cout << "Max Quiz Mark : " << maxQuizMark << std::endl;
	std::cout << "Max Final Exam Mark : " << maxFinalExamMark << std::endl;
	std::cout << "Number of student records : " << numRecords << std::endl;
	std::cout << "----------------------------------" << std::endl;

	// Grab everyones id and data and display it as soon as it was obtained.
	for(int i = 0; i < numRecords; i++ )
	{
		file1 >> studentId[i] >> midExamMark[i] >> projectMark[i] >> quizMark[i] >> finalExamMark[i];
		std::cout << studentId[i] << " " << midExamMark[i] << " " << projectMark[i] << " " << quizMark[i] << " " << finalExamMark[i] << std::endl;
	}

	// Do the calculations here so you would probably want to do something like
	float* totals = new float[numRecords];
	for( int i = 0; i < numRecords; i++ )
	{
		// totals[i] = (float)midExamMark * (float)projectMark * 10;
		//etc
	}
}
Last edited on
Topic archived. No new replies allowed.