help me with code !

Pages: 12
than you Zereo for ur support , we r not allowed to use vectors all library we can use is :

1
2
£include <iostream>
£include <cmath>

thanx again
Then you'll need to use a regular array - but the same principles still apply.

All the best,
NwN

closed account (3qX21hU5)
(HINT: You need to sort the vector or other container)
The key words in there are Other Container. Meaning you can use a array, list, vector any container to store the results of the user entering the grades.

So you can use a array to hold the grades instead of a vector (In my opinion the professor should give you bonus points for using a vector since its up to date code but =/). You can sort a array with a bubble sort algorithm which is what most beginners start out with (More into below).

So this is what you need to do to the code I posted to have it work for your assignment.

1. Replace the vectors with arrays. This is actually a big problem and why vectors are so much better then array for this. Let me explain, when you make a array you need to define how many elements are in that array. Now you might just make a array with 100 elements in it and call it done. That is not correct because when you try and sort that array you will be sorting undefined elements and you will not get the right result.

So what you need to do is everytime you add a element(grade) to the array you need to create a new array and copy all the elements from the old array into it, then delete the old array. This is a lot of work considering in a vector you can just do grades.push_back(n) compared to something like this

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
int main()
{
    int *Old_array(new int[4]);
    for(int I(0); I < 4; ++I)
        Old_array[I] = (I + 1);

    {
        // Resize.
        int *New_array(new int[5]);

        // Copy over.
        for(int I(0); I < 5; ++I)
            New_array[I] = ((I == 4) ? 0 : Old_array[I]);

        // We no longer need the old array.
        delete [ ] Old_array;
        Old_array = New_array;
    }

    for(int I(0); I < 5; ++I)
        std::cout << Old_array[I] << " ";

    delete [ ] Old_array;
}

// Borrowed from Framework since I didn't want to type out a new one. 


Or you can keep track of the number of elements that the user entered grades into with a counter and then only sort up to that number of elements, so you are not sorting uninitialized elements.

Either way would work.

So you can see why vectors would be better.


2) Input the users data into the arrays. You can do this by doing what I said above, create a new array -> Copy the old arrays elements into the new array -> delete the old array.

3) Sort the array. You can use a bubblesort for this since that is what most beginners learn first. There are much better sorting options available also.

4) Find the median. Like I showed in my previous example.
Last edited on
I would still use my snippet of code for the user input because it forces the user to enter a valid grade or quit which is what I think your professors program did if I remember correctly.

MiiNiPaa's code does the calculation no matter what and your (TS) code just go's a 100 times regardless of if they're valid or not. If they make 50 mistakes then they can only enter 50 grades which doesn't seem fair.

It's makes more sense to just set a cap on the amount of valid grades they can enter, which my code does, I think. In fact you could print out the constant studentLimit and studetnCount every time they enter a valid grade to give them an idea of how many grades they entered and how many more they have left.

User are typically stupid and need all the useful info they can get.
Last edited on
closed account (3qX21hU5)
Edit: Was to complicated so deleted so its not a distraction ;p lol.
Last edited on
Use regular array.

I can provide a simple selection sort function which helps you find the median.

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
#include <iostream>
using namespace std;

void selectionSort(int arr[], int count, bool asc=true);
void printElements(int arr[], int count);

int main()
{
    int scores[100] = {70,60,99,45,33,76,88,92,84,42,77,55,75};
    int count = 13; //limited by count

    //initial elements
    printElements(scores, count);

    //sorted ascending
    selectionSort(scores, count);
    printElements(scores, count);

    //sorted descending
    selectionSort(scores, count, false);
    printElements(scores, count);

    return 0;
}


void selectionSort(int arr[], int count, bool asc)
{
    for (int i=0; i<count-1; ++i)
    {
        int iswap = i; //assume this is smallest/biggest
        //Look over remaining elements to find smallest/biggest.
        for (int j=i+1; j<count; ++j)
        {
            if      (  asc && (arr[j]<arr[iswap]) )
                iswap = j; //Remember smallest index for latter swap.
            else if ( !asc && (arr[j]>arr[iswap]) )
                iswap = j; //Remember biggest index for latter swap.
        }
        //Swap smallest/biggest remaining element
        int temp = arr[i];
        arr[i] = arr[iswap];
        arr[iswap] = temp;
    }
}

void printElements(int arr[], int count)
{
    for (int i=0; i<count; ++i)
        cout << arr[i] << " ";
    cout << endl;
}
I understand you are trying to help, but you are over complicating the problem.

Make an array of size 100 and keep track of where you are in it. You could also end the array with the sentinel (-1) and then you wouldn't have to keep track. In this case, make your array size 101. (remind you of a c-string?)

Using a vector for an assignment like this is just an excuse to not think.
Last edited on
closed account (3qX21hU5)
Actually using a vector for a assignment like this is called being smart and using the tools provided.

And I agree tnt that is probably a easier way to go about it. Wasn't sure if that was the standard way of doing it or not always thought creating a new array was easier that is why I only mentioned your method as another way of doing it.
Last edited on
Topic archived. No new replies allowed.
Pages: 12