C 程序适用于 VS Code 但不适用于 Dev-C++
C program works on VS Code but doesn't work on Dev-C++
所以我需要对每位候选人收到的选票求和,但总和变为负数。我尝试在 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
----------------------------------------------------------------------------------------------
你好像忘了在使用前清除局部变量。只需使用 float sum [4] = { 0 };
这是必要的,因为本地分配的内存会被任何值填充。 C 标准感知仅用于清理全局变量。
所以我需要对每位候选人收到的选票求和,但总和变为负数。我尝试在 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
----------------------------------------------------------------------------------------------
你好像忘了在使用前清除局部变量。只需使用 float sum [4] = { 0 };
这是必要的,因为本地分配的内存会被任何值填充。 C 标准感知仅用于清理全局变量。