Sadly I see that a lot where the teacher doesn't explain things properly.
There are quite a few problems with your code, a bad one is that you are calling the functions between 38-45. Had those functions been correct and working they would be run regardless if your memory was allocated or not - not good.
Another is int getMode(int *array, float *data), if you check your assignment it doesn't expect two pointers, and only a pointer to your data and its size (amount of students).
In parts of the code you are using a pointer for the size, that's not required. All you need a pointer for is when working with the studentData since that's a pointer to your data holding the hours.
Try doing it in sections, 1 function at a time as from what I can see you are missing the smaller things such as the getMedian function returning a double and not a float (as per the assignment).
#include <iostream>
usingnamespace std;
int main()
{
// declare it
float *studentData;
// allocate some memory to store 5 hours
studentData = newfloat[5];
// for simplicity i am not checking to see if
// its allocated successfully before someone
// corrects me ;-)
// studentData is essentially a array
// if i wanted to access it like a array, i could
// use the following....
studentData[3] = 134; // array notation
cout << studentData[3] << endl; // display it
// if I was to do the same using pointer notation
*(studentData + 3) = 155; // pointer notation
cout << *(studentData + 3) << endl; // display it
// free the memory
delete[] studentData;
return 0;
}
Okay i reworked all the functions, but my getAverage function is giving me a new error. Error: invalid operands of types 'int' and 'float*' to binary operator/' int *average = newint((numStudents) / data); its in this line.
Alright i fixed that issue the horrible thing compiles but im leaking memory in one of my functions and my output is all wrong! :) but atleast ive made progress.
Not sure why you need to allocate more memory for your getAverage function. All you need to do is loop through the studentData 1 by 1 and add them together, then divide by the number of hours stored held in numStudents.
1 2 3 4 5 6 7 8 9 10 11
float getAverage(float *data, int size)
{
float total = 0;
// add all the hours together
for (int i = 0; i < size; i++)
total += *(data + i);
total = total / size;
return total;
}
In your example where is total being returned to? And i think im starting to get them but im far from understanding :p ill read it hopefully its better than my teacher.
total is returned back to the caller, so in main() in our case. so we could call the average function like this...
1 2 3 4 5 6
float averageResult; // declare top of main.
averageResult = getAverage(studentData, numStudents);
// averageResult now has the average of all the hours.
Yes i have something just like this, problem is the program wont advance to the point where is can output the results. I think my printArray function is all messed up. but that getAverage example fixed the memory leak i think.
C:\C++\Program Three>a
How many students were surveyed? 5
Enter the number of hours each student spent on Facebook and/or Twitter.
Student 1: 12
Student 2: 13
Student 3: 14
Student 4: 13
Student 5: 12
Number of hours each student spent on Facebook / Twitter in ascending order :
0 1 2 3 4 5
Program freezes here, not to mention its not outputting the right numbers above should be sorted hours like:
Number of hours each student spent on Facebook / Twitter in ascending order :
12 12 13 13 14
void printArray(int numStudents, float *data)
{
int x;
x = numStudents;
cout << "\nNumber of hours each student spent on Facebook / Twitter in ascending order : " << endl;
for (x = 0; x <= numStudents; x++)
{
cout << data << " ";
}
}
when i look at it it makes no since, and obviously doesn't work but i cant think of another way to do it. at least this gives some output :p
lines 3 and 4 create and assign x to the number of students.. then reset by the for loop. There is no real use for line 4
You need to simply loop through the data 1 by 1 and print it out using pointer notation. Take a look at the average function and notice my for..loop, obviously instead of adding etc. you simply want to display its contents.
Also note that your for loop is using <= numStudents, you need to treat this like an array where the first index is 0, so for 10 numbers it would be 0-9. You don't want to be landing on 10, know what i mean?
I'm trying to push you gently into the right direction ;-)
Anyway I'm just curious if your teacher has shown you pseudo coding, dry running techniques or flowcharts?
My teacher is how do you say fairly ditsy, she understands what needs to be done but she doesn't really know how to explain it, she writes dummy programs for us all the time in class but being a 8am class she and none of the students are really there. What I have mostly to study is some example programs from my books website. These however are split up amongst like 1000 parts and it's hard to find what I need. I've learned more from you than I have from her all year. I've got class right now but I'll send you what I come up with for that function and see if I'm on the right path.
Where are you from btw, just noticing the time difference.
I don't mind helping you at all, but since pointers are a must for the real world its really important that you have a full understanding of them, and simply writing it for you would be of no benefit - your assignments will only get harder and harder, and you will end up not having a clue at all.
Pointers are probably up there with a few others for being the worst nightmare for people learning programming - your not the first and certainly wont be the last.