Home > database >  C program works on VS Code but doesn't work on Dev-C
C program works on VS Code but doesn't work on Dev-C

Time:03-06

So I need to sum the votes received per candidate but the sum becomes negative. I tried using the debugger in Dev-C but the program stops after I enter the votes so I used VS Code instead to debug the program. But when I run it, the output was correct even though I didn't change any code when I opened it in VS Code. I'm using Dev-C 5.11 and VS Code 1.65.0. I can't switch to VS Code because my teacher requires us to use either Dev-C or Turbo C . I can't use Turbo C as well since it freezes every time I run any program using it.

THE CODE:

/****************************************************************************

MACHINE PROBLEM 1

The results from the mayor's race have been reported by each precinct as
follows:

            Candidate   Candidate   Candidate   Candidate
Precincts   A           B           C           D
1           192         48          206         37
2           147         90          312         21
3           186         12          121         38
4           114         21          408         39
5           267         13          382         29

Write a program to do the following:

a. Print out the table with the appropriate headings for the rows and
    columns.
b. Compute and print the total number of votes received by each
    candidate and the percent of the total votes cast.
c. If any one candidate received over 50 percent of the total votes, the
   program should print a message declaring that candidate the winner.
d. If no candidate received 50 percent of the votes, the program should
    print a message declaring a run-off between the two candidates who
    received the highest number of votes; two candidates should be identified
    by their letter names.
e. Run the program once with the preceding data and once with candidate C
    receiving only 108 votes in precinct 4. (note: test values are not
    limited to the preceding data)

Note: Use Array for this machine problem

*****************************************************************************/

#include <stdio.h>
#include <windows.h>

void headings();
void cHeadings();

char candidates[4] = {'A', 'B', 'C', 'D'};
int precincts[5] = {1, 2, 3, 4, 5};
int votes[5][4];

COORD coord = {0,0};
void gotoxy(int x, int y);

int main()
{
    char border = '|';
    int i, j;
    
    // Print headings

    printf("---------------------------------------------------------------------------------------\n");
    printf("\t\t%c\t", border);
    headings();
    printf("\nPrecincts");
    printf("\t%c\t", border);
    cHeadings();
    printf("---------------------------------------------------------------------------------------\n");
    
    //// Print precinct number
    
    for (i = 0; i < 5; i  ) {
        printf("%d", precincts[i]);
        printf("\t\t%c\t", border);
        if (i != 4)
            printf("\n");
    }
    
    printf("\n---------------------------------------------------------------------------------------\n");

    // Table data
    
    int xcoord = 24, ycoord = 4;
    
    for (i = 0; i < 5; i  , ycoord  ) {
        xcoord = 24;
        for (j = 0; j < 4; j  , xcoord  = 16) {
            gotoxy(xcoord, ycoord);
            scanf("%d", &votes[i][j]);
        }
        printf("\n");
    }
    
    // Total number of votes per candidate

    float sum[1][4];

    printf("\n");
    printf("----------------------------------------------------------------------------------------------\n");
    printf("\t\t\t%c\t", border);
    headings();
    printf("\t\t\t\t\t%c\t", border);
    cHeadings();
    printf("----------------------------------------------------------------------------------------------\n");
    printf("Total no. of votes\t");
    printf("%c\t", border);
    for (i = 0; i < 5; i  ) {
        //j = 0;
        for (j = 0; j < 4; j  ) {
            sum[0][j]  = votes[i][j];
        }
    }
    for (i = 0; i < 4; i  ) {
        printf("%d\t\t", (int) sum[0][i]);
    }
    
    printf("\n----------------------------------------------------------------------------------------------");

    return 0;
}

// Print "Candidate" headings

void headings()
{
    int i;
    for (i = 0; i < 4; i  ) {
        /*if (i == 0) {
            printf("\t");
        } else {*/
            printf("Candidate\t");
        //}
        if (i == 4) {
            printf("\n");
        }
    }
}

// Print candidate letter names

void cHeadings()
{
    int i;
    for (i = 0; i < 5; i  ) {
        printf("%c\t\t", candidates[i]);
        if (i == 4) {
            printf("\n");
        }
    }
}

// gotoxy()

void gotoxy(int x, int y)
{
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

OUTPUT (DEV-C ):

---------------------------------------------------------------------------------------
                |       Candidate       Candidate       Candidate       Candidate
Precincts       |       A               B               C               D
---------------------------------------------------------------------------------------
1               |       192             48              206             37
2               |       147             90              312             21
3               |       186             12              121             38
4               |       114             21              408             39
5               |       267             13              382             29
---------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------
                        |       Candidate       Candidate       Candidate       Candidate
                        |       A               B               C               D
----------------------------------------------------------------------------------------------
Total no. of votes      |       -2147483648             -2147483648             1429            164
----------------------------------------------------------------------------------------------
--------------------------------
Process exited after 43.82 seconds with return value 0
Press any key to continue . . .

OUTPUT (VS CODE):

---------------------------------------------------------------------------------------
                |       Candidate       Candidate       Candidate       Candidate
Precincts       |       A               B               C               D
---------------------------------------------------------------------------------------
1               |       192             48              206             37
2               |       147             90              312             21
3               |       186             12              121             38
4               |       114             21              408             39
5               |       267             13              382             29
---------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------
                        |       Candidate       Candidate       Candidate       Candidate
                        |       A               B               C               D
----------------------------------------------------------------------------------------------
Total no. of votes      |       906             184             1429            164
----------------------------------------------------------------------------------------------

CodePudding user response:

Seems that you forgot to clean local variables before to use. Just use float sum [4] = { 0 };

It is necessary due to memory which is allocated locally would be filled by any values. C standard aware just for cleaning of global variables.

  • Related