Homework Problem

I'm a beginner in C++ and I already feel lost. My teacher assigned us this problem:

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

Optional:
Create a Dynamic Array (on the heap) to hold a set of four test scores for five students. (Hint: you must write your own access algorithm).

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.

If anyone could just help me get started, I would greatly appreciate it.
Last edited on
Try it out yourself. If you face any specific problem, then ask here.

If you are totally lost, then you should take help from your professor or fellow-mates.
Creating a variable "on the stack" basically means that it's a local variable in a function ("automatic memory allocation").
http://www.cplusplus.com/forum/beginner/18101/

1
2
3
4
5
void func()
{
    int a; // on the Stack
    auto int b; // same as above, the keyword "auto" is useless
}


Global (and static) variables do not live on the stack. They are allocated statically.
http://en.wikipedia.org/wiki/Static_memory_allocation
http://en.wikipedia.org/wiki/Static_variable

1
2
3
4
5
6
int a; // not on the Stack or Heap

void func()
{
    static int b; // not on the Stack or Heap
}


Creating a variable "on the heap" basically means that you use dynamic memory allocation (the new and delete operators).
http://www.cplusplus.com/doc/tutorial/dynamic/

1
2
3
4
5
6
7
8
void func()
{
    int *a = new int[100]; // dynamically allocate 100 int's on the Heap

    // ... use them...

    delete[] a; // free the memory once no longer needed
}


Dynamic memory allocation is used because the Heap is much larger than the Stack. So if you need to allocate large amounts of memory, it's a bad idea to allocate them on the Stack.
http://en.wikipedia.org/wiki/Stack_overflow

With that out of the way...

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


int scores[5][4];

Undefined95 wrote:
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

Did you learn about struct's? Are you allowed to use struct's in your homework? They would simplify things a lot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>

struct Student
{
    std::string name;
    std::string id;
    float average_score;
    std::string letter_grade; // not char, in order to allow for things like "A+" maybe
};

// ...

Student *ps = new Student[x]; // where x is how many students you read data about

// ...

delete[] ps;


Undefined95 wrote:
If anyone could just help me get started, I would greatly appreciate it.

You may want to read through the tutorial on this site.
http://www.cplusplus.com/doc/tutorial/
I have not yet learned about struct's. Is there a different way to go about the problem?
All the more reason to get help from your instructor or classmates. We have a policy not to do homework for people. We will help you if you post code to show what you have done so far and explain what you are stuck on, but we won't do the work for you. Us doing it won't benefit you at all and will just make you dependent on us or others to continue to do the work, especially if you don't take the time to understand the code.
you just know the basics?i mean just arrays?
I do not want anyone to do my homework for me. All I wanted was some input/advice on how to go about the problem. I do know the basics/arrays.

Thats a good attitude to have, simply getting someone to do it for you would be of no benefit. Anyway, catfish666 has given a nice explanation but alongside his/her post I have added some code below.

Its worth checking if you get more marks when using dynamically allocated arrays... more marks is always good :)

However, the example below is statically allocated.

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

#include <iostream>

int main()
{

	// array of 5 down (students) by 4 across (scores) - remember arrays start at 0
	int scores[5][4];	

	/*
	
		So scores will look like this..

				[0][0-3]  1st Student, 4 test scores
				[1][0-3]  2nd Student, 4 test scores
				[2][0-3]  3rd Student, 4 test scores
				[3][0-3]  4th Student, 4 test scores
				[4][0-3]  5th Student, 4 test scores

		So if I wanted to access student 3 and the 2nd score
		for that student i would use index [2][1] as arrays 
		start at 0 not 1

		so reading it:

		studentScore = scores[2][1]; 

		or writing to it:

		scores[2][1] = 4;
	
	*/

	return 0;
}


Using For loops you could go through each student 1 by 1 getting their 4 scores quite easily. The following links may be helpful to you:

Arrays: http://www.cplusplus.com/doc/tutorial/arrays/
Flow Control: http://www.cplusplus.com/doc/tutorial/control/

Thanks and at this point I'm going for the static 2D array instead of the dynamic array since I don't quite understand those yet.
Undefined95 wrote:
Thanks and at this point I'm going for the static 2D array instead of the dynamic array since I don't quite understand those yet.

Dynamic arrays are allocated on the heap.
In C++ you use the operator new[] to allocate them, and operator delete[] to deallocate them.

When you use new[], it allocates an array in memory and returns the memory address of the first element. You need to "save" the memory address, and you do so by putting it in a pointer (a pointer being a variable that stores a memory address).

You must keep the memory address, because later you'll need to feed it to delete[] in order to deallocate the dynamic array.

This is valid in C and C++. Newer languages (C#, Java, D) don't require you to release the memory manually, because they have a garbage collector. The purpose of a GC is exactly that: to automatically release unused resources.
Oh okay. Thank you for explaining that. My problem now is just getting the data from my file to be read into the different arrays I am trying to create.
Undefined95 wrote:
My problem now is just getting the data from my file to be read into the different arrays I am trying to create.

Read this section of the tutorial please:
http://www.cplusplus.com/doc/tutorial/files/

Try it yourself and when/if it doesn't work, post your code here and say what's wrong.
Don't forget about putting your code in [code][/code] tags:
http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.