Need Urgent answers for this questions !

* So i got this 2 questions, homework in school i got to cout what it asks, but my professor says i did not follow all the instructions: I should mention i'm a newbie to c++ and also to this language, English.
Does anyone have a similar code to this questions ? My professor said that i did not write the function. and that for the second question i did not use it as if it was 2D. Please Help!! i have a test tomorrow on this.

1) Write a Function that will return the average salary in a file of Data records. The Function will receive the file name as string and check if the file does not exist, return -1 otherwise return the average salary. (Do Not write the complete program)

struct Data { char name[20]; double Salary; int age; } // In this order

2) Create a function that will find and return the mean of an M by N, 2 dimensional integer array. The function receives no arguments and will randomly set N and M from 1-10. It will create the 2 dimensional array and randomly initialize it with values from 3-55. (Assume all headers are Included)

1
2
3
4
//also not sure if 2D is like this on an array
int a[N*M];
M = srand()%53 + 3;
M = srand()%53 + 3;
Last edited on

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
signed int getAverageSalary(std::string filename)
{
   signed int nSalary = 0;
   signed int Sum = 0;

    std::ifstream file(filename);
    if(!file.good())
       return -1;
    while(!file.eof())
    {
         Data data;
         file >> data.name;
         file >> data.Salary;
         file >> data.age;

        Sum += data.Salary;
        nSalary ++;
     }
    return (signed int)(Sum / nSalary);
}

unsigned int getMean(void)
{
     unsigned int M = 1 + rand()% 10;
     unsigned int N = 1 + rand()% 10;

    unsigned int mean = 0;

     unsigned int** Array = new unsigned int * [M];
    for(unsigned int i = 0 ; i < M ; ++i)
   {
      Array[i] = new unsigned int[N];
      for(unsigned int j = 0; i < N ; ++j)
      {
            Array[i][j] = 3 + rand()%53;
       }
   }
    //calculate the mean
   //mean = something;

   //make sure to delete the matrix
  for(unsigned int i = 0 ; i < M ; ++i)
    delete[] Array[i];

  delete[] Array;

  //return the mean
  return mean;
}
so in other words what i had to do is write them as prototypes so not inside main? is that what the question is asking me to do. Thanks man by the way you are the man. save my life. lol
One more question if you dont mind, Why use signed or unsigned ? my professor has not explained that yet but i would like to know from a wise person like you.
theres something wrong with this but it doesnt compile it says bad access.

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
int getMean(void)
{
    srand(time(NULL));
    int M = 1 + rand()% 10;
    int N = 1 + rand()% 10;
    
    int mean = 0;
    
    unsigned int ** Array = new unsigned int * [M];
    for(unsigned int i = 0 ; i < M ; ++i)
    {
        Array[i] = new unsigned int[N];
        for(unsigned int j = 0; i < N ; ++j)
        {
            Array[i][j] = 3 + rand()% 53;
// this one does not compile it says: Thread 1: EXC_BAD_ACCESS
        }
    }

    for(int i = 0 ; i < M ; ++i)
        delete[] Array[i];
    
    delete[] Array;
//If i put only this without the for loop above would it still delete the array?

    return mean;
}
it did not compile it yet , I check it. But srand goes at the beginning of the main.
I putted i instead of j

that should work :

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
#include <fstream>

struct Data
{
	char name[20];
	double Salary;
	int age;
};
signed int getAverageSalary(std::string filename)
{
	signed int nSalary = 0;
	double Sum = 0.0;

	std::ifstream file(filename);
	if (!file.good())
		return -1;
	while (!file.eof())
	{
		Data data;
		file >> data.name;
		file >> data.Salary;
		file >> data.age;

		Sum += data.Salary;
		nSalary++;
	}
	return (signed int)(Sum / nSalary);
}

unsigned int getMean(void)
{
	unsigned int M = 1 + rand() % 10;
	unsigned int N = 1 + rand() % 10;

	unsigned int mean = 0;

	unsigned int** Array = new unsigned int *[M];
	for (unsigned int i = 0; i < M; ++i)
	{
		Array[i] = new unsigned int[N];
		for (unsigned int j = 0; j < N; ++j)
		{
			Array[i][j] = 3 + rand() % 53;
		}
	}
	//calculate the mean
	//mean = something;

	//make sure to delete the matrix
	for (unsigned int i = 0; i < M; ++i)
		delete[] Array[i];

	delete[] Array;

	//return the mean
	return mean;
}

int main(int argc, char* argv[])
{
srand(time(NULL));

	getMean();

	return 0;
}
Last edited on
Topic archived. No new replies allowed.