0

I'm outputting a two-dimensional array with a for loop like this:

void output(int data[50][8], string names[50]) {
    int amount = 0;
    amount = fillAndDisplay(data, names);`

    for (int i = 0; i < amount; i++) {
        cout << names[i] << " ";

        for (int j = 0; j < 8; j++) {
            cout << data[i][j] << " ";
        }

        cout << endl;

    }

    return;
}

amount will equal 5 in my case.

Now, right now I'm returning data and names, but it's not working. What do I need to return? Also, what data type should I use for the function? I'm using void right now, but I'm not sure if that's correct.

Here's my full code. The file that it's reading in is:

5 
Franks,Tom 2 3 8 3 6 3 5 
Gates,Bill 8 8 3 0 8 2 0 
Jordan,Michael 9 10 4 7 0 0 0 
Bush,George  5 6 5 6 5 6 5 
Heinke,Lonnie  7 3 8 7 2 5 7

Here's the code:

int fillAndDisplay(int data[50][8], string names[50]);

void sort(int data[50][8], string names[50]);

void output(int data[50][8], string names[50]);

int main()
{

int data[50][8];

string names[50];

int amount = 0;


 amount = fillAndDisplay(data, names);

 sort(data, names);

 output(data, names);

system("pause");
return 0;

}

int fillAndDisplay(int data[50][8], string names[50]) {

int const TTL_HRS = 7;
ifstream fin;
fin.open("empdata.txt");

if (fin.fail()) {
    cout << "ERROR";
}

int sum = 0;
int numOfNames;
fin >> numOfNames;

for (int i = 0; i < numOfNames; i++) {

    fin >> names[i];

    data[i][7] = 0;

    for (int j = 0; j < 7; j++) {
        fin >> data[i][j];
        data[i][TTL_HRS] += data[i][j];
    }
}

return numOfNames;

}

void sort(int data[50][8], string names[50]) {

int amount = 0;
int temp = 0;
bool hasSwapped = true;
string tempName;

amount = fillAndDisplay(data, names);

while (hasSwapped) {

    hasSwapped = false;

    for (int i = 0; i < amount - 1; ++i)
    {
        if (data[i][7] < data[i + 1][7]) {

            for (int j = 0; j <= 7; j++) {

                temp = data[i][j];
                data[i][j] = data[i + 1][j];
                data[i + 1][j] = temp;
            }
            tempName = names[i];
            names[i] = names[i + 1];
            names[i + 1] = tempName;

            hasSwapped = true;

        }

    }
}
}

void output(int data[50][8], string names[50]) {

int amount = 0;
amount = fillAndDisplay(data, names);

for (int i = 0; i < amount; i++) {
    cout << names[i] << " ";

    for (int j = 0; j < 8; j++) {
        cout << data[i][j] << " ";
    }

    cout << endl;

}


}

When The code to print out the arrays is in a function it prints out:

Franks,Tom 2 3 8 3 6 3 5 30
Gates,Bill 8 8 3 0 8 2 0 29
Jordan,Michael 9 10 4 7 0 0 0 30
Bush,George 5 6 5 6 5 6 5 38
Heinke,Lonnie 7 3 8 7 2 5 7 39
Press any key to continue . . .

but when the code is just in main it prints out

Heinke,Lonnie 7 3 8 7 2 5 7 39
Bush,George 5 6 5 6 5 6 5 38
Jordan,Michael 9 10 4 7 0 0 0 30
Franks,Tom 2 3 8 3 6 3 5 30
Gates,Bill 8 8 3 0 8 2 0 29
Press any key to continue . . .

Which is what it should print out.

  • Why do you believe you need to return anything? This ``int fillAndDisplay(int data[50][8], string names[50]);`` is actually passing pointers to the existing data, not making copies of your arrays: – PaulMcKenzie Nov 03 '16 at 04:20
  • what does passing pointers mean? – Will Parks Nov 03 '16 at 04:23
  • When you "pass arrays" in C++, you are actually passing pointers to the first element. The syntax you're using is fooling you into thinking you're actually passing arrays, when you're not. In other words (and this is a fundamental in C++) arrays decay to pointers when passed. – PaulMcKenzie Nov 03 '16 at 04:24
  • oh. how do I make it work then, because the sort function works fine and I'm doing the same thing – Will Parks Nov 03 '16 at 04:27
  • What sorting algorithm are you using? If it's a bubble sort, it's totally wrong. If you don't know what type of sort it is, then that's one issue you need to resolve, as writing sorts "off the cuff" without knowledge of one of the classical sorting algorithms usually doesn't work out. – PaulMcKenzie Nov 03 '16 at 04:29
  • yeah...it's supposed to be bubble sort. That may be where I've went wrong, but it worked fine when the fillAndDisplay function was just in main – Will Parks Nov 03 '16 at 04:32
  • Well, the sort is wrong. You swap items that don't need to be swapped, you compare an item outside of the inner loop (the bubble sort does not do this), etc. – PaulMcKenzie Nov 03 '16 at 04:34
  • Ok. Do you think that's the problem because like I said before it worked fine when fillAndDisplay was in main. I can try to make it into a correct bubble sort once I've got the functions figured out – Will Parks Nov 03 '16 at 04:38
  • If you write your sort correctly then you won't need to return anything, as I stated previously that you're working with the original arrays, not copies. – PaulMcKenzie Nov 03 '16 at 04:40
  • How would you fix the sort because that's the only way I could get it to work. – Will Parks Nov 03 '16 at 04:42
  • Your sort doesn't follow the classic bubble sort algorithm. Start there first. It is way off. – PaulMcKenzie Nov 03 '16 at 04:43
  • I thought bubble sort was comparing one element of the array to another and then comparing the smaller one to the next and so on. (I'm only comparing the last element in each array and sorting them based on that btw) – Will Parks Nov 03 '16 at 04:47
  • A bubble sort doesn't compare and potentially swap items outside of the inner loop. [See this](http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm). A classic bubble sort is a double nested loop, and within the inner nesting is where the comparisons and swapping takes place. Look at your implementation and you will see the difference. – PaulMcKenzie Nov 03 '16 at 04:48
  • I'm still not seeing what I'm supposed to be doing...should I be swapping names in the inner loop as well? – Will Parks Nov 03 '16 at 04:52
  • May be helpful reading: [What's the difference between passing by reference vs. passing by value?](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) A pointer is a type of reference. – user4581301 Nov 03 '16 at 04:56
  • 1) You shouldn't be making any comparisons outside of the inner loop. 2) You have to swap the names along with the data when the data is swapped and at no other time. If not, how will the data and names be in synch? And the link I gave you shows an example of a bubble sort. Do you see it comparing items outside of the inner loop? All the comparisons are done within the inner nesting. – PaulMcKenzie Nov 03 '16 at 04:57
  • But I'm swapping two different arrays.... – Will Parks Nov 03 '16 at 05:01
  • voted to close as **unclear**. requests for code review should go to the code review site. requests for debug-my-program should not be made. – Cheers and hth. - Alf Nov 03 '16 at 05:06
  • Save yourself some trouble. Build a structure that contains a name and the associated numbers. and use an array of that structure. Now you have only one array to deal with and your swapping logic will get much, much simpler. – user4581301 Nov 03 '16 at 05:06
  • If you are having trouble visualizing how your program is executing, virtually every development environment [comes with a debugger](https://en.wikipedia.org/wiki/Debugger). This handy program allows you to control the execution of your program, stepping through it line by line if need be, to see what your program is doing and why. It is quite possible that you will never again in your life find a better productivity tool, so the sooner you learn how to use it, the better off you will be. – user4581301 Nov 03 '16 at 05:10

0 Answers0