所以我需要將每個候選人收到的選票相加,但總和變為負數。我嘗試在 Dev-C 中使用除錯器,但程式在我輸入投票后停止,所以我使用 VS Code 來除錯程式。但是當我運行它時,即使我在 VS Code 中打開它時沒有更改任何代碼,輸出也是正確的。我正在使用 Dev-C 5.11 和 VS Code 1.65.0。我無法切換到 VS Code,因為我的老師要求我們使用 Dev-C 或 Turbo C 。我也不能使用 Turbo C ,因為每次我使用它運行任何程式時它都會凍結。
代碼:
/****************************************************************************
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);
}
輸出(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 . . .
輸出(VS 代碼):
---------------------------------------------------------------------------------------
| 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
----------------------------------------------------------------------------------------------
uj5u.com熱心網友回復:
似乎您忘記在使用之前清理區域變數。只需使用float sum [4] = { 0 };
這是必要的,因為本地分配的記憶體將被任何值填充。C 標準僅用于清理全域變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438790.html
上一篇:上次更新后VSCode主題壞了
