multidimensional arrays/2d arrays

HEY my name is Vanessa and i need help big time on an assignment to add to my gpa. The question is as follows. I have no idea what to do. PLEASE HELP!!??? (Oh and this is for structured programming)

Write a C++ program to read from a file called “input.txt” information about 5 workers. Each line of input would
contain two values respectively, an employee ID and the employee’s monthly salary.
Assume that the data from the input file is stored in a format as shown in this example. Note: for testing purposes, the
following data may not necessarily be used. It is just to give you an idea of the format of the data:
Sample contents of input.txt:
10 2000
20 6800
30 9700
40 1500
50 8000
Store the employee information into a 2D array. Assume that the data types are as follows: id (integer data type) and income
(integer data type).
Read the information and store the 5 employee records into the 2D array.
Use a function that prints the sum of all incomes of workers.
Create another function that prints the number of workers who has an income of greater than $5000 and also
the ID and income of each worker who has an income of greater than $5000.
Sample output printed to the screen:
Sum of all incomes: $ 24500
3 Workers have an income of greater than $5000:
20 $6800; 30 $9700; 50 $8000
These might help:
http://www.cplusplus.com/doc/tutorial/files/
http://www.cplusplus.com/doc/tutorial/arrays/

Show us some effort on your part, at least some code even if it doesn't work, then we'll help you.
Last edited on
Hi,
Well, best place is at the point where you realise this isn't a homework site and showing us where you are having problems etc with work you have put in. Cut and paste the assignment doesn't count.

You can also post it in the Job section if you want someone to do it entirely for you.

Everybody here is glad to help when you get off the starting blocks :)
Last edited on
You can post it in the job section if you look for someone to do it for you.
It's a simple job so it should not be more than a few dollars.

http://www.cplusplus.com/forum/jobs/
I did try something but i know for sure it's wrong. this is one of the ways i tried the first part of getting the program to prompt the user to enter data to store it in a file

#include <stdio.h>
#include <iostream>
using namespace std;
main () {
FILE * a = fopen ("input.txt", "w");
int employee[5][2]= {{ },{ }, { },{ },{ }};

for (int row=0; row<5;row++) {
for (int column=0;column<2;column++){
printf ("Please enter id and salary\n");
scanf ("%d, %d", &employee);
printf ("%d, %d", employee[row][column]);

}

}
}


My lecturer didn't touch on this topic with us at all so I relied on the web for help. This is just part that I tried I can't move on if this doesn't work and it isn't working
FILE * a = fopen ("input.txt", "w"); You need to open the file for reading not writing.

1
2
3
4
5
for (int row=0; row<5;row++) {
for (int column=0;column<2;column++){
printf ("Please enter id and salary\n");
scanf ("%d, %d", &employee);
printf ("%d, %d", employee[row][column]);
You need to read the data from the file not from the keyboard

int employee[5][2]= {{ },{ }, { },{ },{ }}; much simpler is int employee[5][2]= {0};

Here is an example how you can read the data from the file.
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
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main () 
{
  FILE * a = fopen ("input.txt", "r");

  if (a == 0)
  {
    perror ("File error: ");
    exit (-1);
  }

  int employee[5][2] = { 0};

  // read the data from the file into the array employee

  for (int row = 0; row < 5; row++)
  {
    for (int column = 0; column < 2; column++)
    {
      int tmp;
      fscanf (a, "%u", &tmp);
      employee[row][column] = tmp;
    }
  }
  // display the data from the array employee - just for testing
  for (int row = 0; row<5; row++)
  {
    for (int column = 0; column<2; column++)
    {
      printf("%d ", employee[row][column]);
    }
    printf ("\n");
  }

  system ("pause");
  return 0;
}

Output:
1
2
3
4
5
6
10 2000
20 6800
30 9700
40 1500
50 8000
Press any key to continue . . .
Topic archived. No new replies allowed.