Populating arrays from files.

Pages: 12
So I seem to be confused on how to populate an array (2D and an array on the heap) here is what I am suppose to do:

Create a Static 2D Array on the Stack to hold a set of four test scores for five students.

Create dynamic parallel arrays (on the heap) to hold student names, student id, average score, and a letter grade based on standard grade scale.

Populate the arrays from the file Student Data.txt

Write a program to do the following tasks:

1. Calculate average score for each student.
2. Assign letter grade for each student.
3. Display name, id, scores, average, and grade for each student.
4. calculate the class average score and display.

I know I have not yet done 1-4 in my code but I need help on how I would populate the arrays. Any help would be great! thanks!



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//  main.cpp
//  Program 8
//  Created by William Blake Harp on 7/16/14.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <vector>

int size;
int sum_of_test_scores;
double average;
double class_average;

using namespace std;

int main()
{
    
    ifstream inputFile;
    
    string data = "Student Data.txt";
    
    //2D Array.
    const int students = 5;
    const int test_scores = 4;
    int s[students][test_scores];
    
    //Arrays on the heap.
    int* arrayNames = nullptr;
    arrayNames = new int[size];
    
    int* arrayID = nullptr;
    arrayID = new int[size];
    
    int* arrayAverage = nullptr;
    arrayAverage = new int[size];
    
    int* arrayLetterGrade = nullptr;
    arrayLetterGrade = new int[size];
    
    // Open the file
    inputFile.open("data.c_str");
    
    // Populating 2D array test_scores.
    for ((int i = 2; i != test_scores; i % 2) == 0)
    {
        cout << inputFile << endl;
    }
    
    // Populating array "arrayNames"
    for ((int i = 0; i != arrayNames; i % 2) == 0)
    {
        cout << inputFile << endl;
    }
    
    // Populating array "arrayID"
    for ((int i = 1; i != arrayID; i % 2) == 0)
    {
        cout << inputFile << endl;
    }
    
    
    
    
    
    
    // Close the file.
    inputFile.close();
    
    return 0;
}// End Code.

Last edited on
I was reading that link and I don't think thats exactly what i am looking for. I need to know how to take some information in a file (the data in the file is below) and organize them in different arrays. That is my issue. once I get to that point I am almost positive I can take that data find and display what I need. If any one could give me an example of this please I would greatly appreciate it!



Amy Adams
10111
97 86 78 95
Ben Barr
20222
89 81 73 87
Carla Carr
30333
79 71 63 77
Don Davis
40444
69 62 58 67
Edna Eaton
50555
63 51 62 48
I was reading that link and I don't think thats exactly what i am looking for.

Maybe so. but it should help you avoid coming up with code like this:
 
     cout << inputFile << endl;
I understand I am doing it wrong LOL I see that I just need some kinda example of how to do it right. Thus to eliminate all of my frustration :)
Well, if that tutorial doesn't help, perhaps you need to step back to this more basic one.
http://www.cplusplus.com/doc/tutorial/basic_io/

It's difficult to more on to advanced topics without first grasping the fundamentals.

The line of code I quoted last time is directly analogous to this:
 
    cout << cin << endl;

If you see what is wrong with that, you should be able to see what's wrong with the other code too.


I'm working on the exact same problem as you are. Are you having trouble reading info from the data into the array as well?
@Undefined95 Yes! its frustrating :)
Yes it really is. Is your data in a ".txt" file? or a ".rtf" file?
.txt
I think that might be the problem with mine. I have a "rtf" file.
yes I think that is definitely one one of your problems. But you think you got the code right that tells the file to put the data into the different arrays?
No. I think the code I have won't divide the data into different arrays. It'll just put everything into the same array.
I had that problem too a min ago but I found this:

Declare 2D Array

1
2
const int R = 4; const int C = 3;
int s[R][C] ;


Define & Initialize 2D Array

1
2
const int R = 4; const int C = 3;
int s[R][C] = { {11, 11, 11}, }; {22,22,22}


Display Contents of 2D Array
use nested for-Loops to process 2D Arrays

1
2
3
4
5
6
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
cout << s[i][j] <<" "; cout<<endl;
}



Populate 2D Array

1
2
3
4
for(int i=0; i < R; i++)
{
cout<<"Enter 3 scores for set "<<(i+1)<<": "; for(int j=0; j < C; j++)
cin >> s[i][j]; }


Are you using both 2D and arrays on the heap? or pointer's?
Last edited on
Well the dynamic array on the heap was actually optional for my class so I chose to do a 2D static array on the stack instead.
yes same I am doing the 2D as well. But I thought you still had to do the 2nd part of the program using the heap?
Oh I see what you're saying. I just re-read the problem. I guess you do still have to use the heap afterall....
yea and I can not get past the point of populating them this is what I have and I don't know what I am doing wrong but nothing is displaying when I run it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Populating 2D array.
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < test_scores; j++)
        {
            cin >> s[i][j];
        }
    }
    
    // Displaying 2D array.
    for (int i = 0; i < r; i++)
    {
        for(int j = 0; j < test_scores; j++)
        {
            cout << s[i][j];
        }
        cout << endl;
    }
The same thing happened with me. I would type in the code and the dialog box showed up empty. But anyway, earlier when I posted this question, a couple of people posted some hints on how to get started. It didn't help me out much, but perhaps you'll find some use in it. I'm pretty much done for now. I'll probably just have to attend tutorials tomorrow to figure it out. Good luck!
ok good luck! I will take a look at it!
Pages: 12