-1

Part of my homework is to manipulate a 2D array filled with team scores using functions. I'm having trouble with the function where they want me to print all scores ordered by opponents from least to greatest. Here is what I have.

#include <stdio.h>
void arrangeScores(int array[][50], int team){
    int low = array[0][0], tempArray[2][50], i, j = 0, instance = 1, flag, done = 0;

    while(done == 0){ //perform while list is being sorted
        for(i = 1; i < 50; ++i){ //checks for low value skipping flag number
            if(array[team][i] == -999)
                continue;
            else if(array[team][i] < low)
                low = array[0][i];
            else
            ++instance; //handles duplicates
        }
        while(instance != 0){ //loads temp array
            tempArray[team][j] = low;
            for(i = 0; i < 50; ++i){
                if(array[team][i] == low){
                    tempArray[team + 1][j] = array[team + 1][i];
                    array[team][i] = -999;
                }
            }
            ++j;
            --instance;
        }
        instance = 1;
        flag = 0;
        for(i = 0; i < 50; ++i){ //checks to see if list is all flags(empty)
            if(array[team][i] == -999)
                ++flag;
            else if(flag == 49)
                done = 1;
        }
        for(i = 0; i < 50; ++i) //updates low to new number
            if(array[team][i] != -999 && array[team][i] != low){
                low = array[team][i];
                break;
            }
        }
        for(i = 0; i < 50; ++i){ //reinitalizes origional array from sorted numbers
            array[team][i] = tempArray[team][i];
            array[team + 1][i] = tempArray[team + 1][i];
        }
    }
    int main(){
        int array[2][50];
        int j = 0;
        
        for(int i = 50; i > 0; --i){
            array[0][j] = i;
            ++j;
        }
        for(int i = 0; i < 50; ++i){
            array[1][i] = i;
        }
        
        arrangeScores(array, 0);
        for(int i = 0; i < 50; ++i)
            printf("%d %d\n", array[0][i], array[1][i]);
            
        return 0;
}

For some reason every element in the 2nd row are all 50.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 2
    Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Mar 02 '23 at 02:27
  • 1
    You code is intended incorrectly. It makes it harder for yourself than necessary. – Allan Wind Mar 02 '23 at 04:16
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 02 '23 at 05:37
  • Im trying to sort the items in one side of the array from low to high while keeping the corresponding value in the second side of the array together with the partner value. so if array[0][1] is moved array [1][1] need to move with it – Elijah Carnley Mar 02 '23 at 16:01

1 Answers1

0

You haven't told us what arrangeScores() is supposed to do so this restricts us to a technical analysis. The last thing the function does is:

    for(int i = 0; i < 50; ++i){ //reinitalizes origional array from sorted numbers
        array[team][i] = tempArray[team][i];
        array[team + 1][i] = tempArray[team + 1][i];
    }

This means we need to figure out why the right hand side tempArray[team + 1][i] = 50. Let's use a gdb watch point:

$ gcc -g3 1.c
$ gdb ./a.out
(gdb) b arrangeScores
(gdb) r
(gdb) wa tempARray[1][0] == 50
(gdb) c
Continuing.

Hardware watchpoint 2: tempArray[1][0] == 50

Old value = 0
New value = 1
arrangeScores (array=0x7fffffffdce0, team=0) at 1.c:16
16                              for(int i = 0; i < 50; i++) {

So the value changed just before the for-loop:

        while(instance){ //loads temp array
            tempArray[team][j] = low;
            for(int i = 0; i < 50; i++) {
                if(array[team][i] == low){
                    tempArray[team + 1][j] = array[team + 1][i];
                    array[team][i] = -999;
                }
            }
            j++;
            instance--;
        }

Let's look closer at tempArray[team][j] = low:

(gdb) p team
$24 = 0
(gdb) p j
$25 = 50

Ah... tempArray[2][50] so you have a logic error with j is out of bounds, specifically:

(gdb) p &tempArray[0][50]
$27 = (int *) 0x7fffffffdbd8
(gdb) p &tempArray[1][0]
$28 = (int *) 0x7fffffffdbd8

It turns out we can also have the compiler tell us:

$ gcc -g3 -fsanitize=undefined 1.c 
$ ./a.out
1.c:15:19: runtime error: index 50 out of bounds for type 'int [50]'
1.c:15:23: runtime error: store to address 0x7ffc6739dfb0 with insufficient space for an object of type 'int'
0x7ffc6739dfb0: note: pointer points here
 32 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 32 00 00 00  32 00 00 00 32 00 00 00  32 00 00 00
Allan Wind
  • 23,068
  • 5
  • 28
  • 38