[英]How do I add a Y/N to the following code?
I am learning from C Programming Absolute Beginner's Guide.我正在学习 C 编程绝对初学者指南。 I had to do the following exercise and I took it upon myself to try to add a:
我不得不做以下练习,我自己尝试添加一个:
"Would you like to sort these numbers in ascending order? (Y/N): " “您想按升序对这些数字进行排序吗?(是/否):”
after the numbers are randomly generated.数字随机生成后。 However, I am struggling with this.
但是,我正在为此而苦苦挣扎。 I have tried if, else if, do while, etc. But I am struggling with it.
我已经尝试过如果,否则如果,做一段时间等。但我正在努力解决它。 How would you add that to this code?
您将如何将其添加到此代码中?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int ctr, inner, outer, didSwap, temp, choice;
int nums[10];
time_t t;
srand(time(&t));
//First step is to fill the array with random numbers (from 1 to 10)
for (ctr = 0; ctr < 10; ctr++)
{
nums[ctr] = (rand() % 99 + 1);
}
//Now list the array as it currently is before sorting
puts("\nHere is the list before the sort:");
for (ctr = 0; ctr < 10; ctr++)
{
printf("%d\n", nums[ctr]);
}
//Now sort the array
for (outer = 0; outer < 10; outer++)
{
didSwap = 0; //Becomes 1 (true) if list is not yet ordered
for (inner = outer; inner < 10; inner++)
{
if (nums[inner] < nums [outer])
{
temp = nums[inner];
nums[inner] = nums [outer];
nums[outer] = temp;
didSwap = 1;
}
}
if (didSwap == 0)
{
break;
}
}
//Now list the array as it currently is after sorting
puts("\nHere is the list after the sort:");
for (ctr = 0; ctr <10; ctr++)
{
printf("%d\n", nums[ctr]);
}
return 0;
}
EDITED CODE:编辑代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int ctr, inner, outer, didSwap, temp, choice;
int nums[10];
time_t t;
srand(time(&t));
//First step is to fill the array with random numbers (from 1 to 10)
for (ctr = 0; ctr < 10; ctr++)
{
nums[ctr] = (rand() % 99 + 1);
}
//Now list the array as it currently is before sorting
puts("\nHere is the list before the sort:");
for (ctr = 0; ctr < 10; ctr++)
{
printf("%d\n", nums[ctr]);
}
puts("Would you like to sort in ascending order? ");
puts(("(Reply Y or N): "));
scanf(" %d", choice);
choice = toupper(choice);
if (choice = 'Y')
{
//Now sort the array
for (outer = 0; outer < 10; outer++)
{
didSwap = 0; //Becomes 1 (true) if list is not yet ordered
for (inner = outer; inner < 10; inner++)
{
if (nums[inner] < nums [outer])
{
temp = nums[inner];
nums[inner] = nums [outer];
nums[outer] = temp;
didSwap = 1;
}
}
if (didSwap == 0)
{
break;
}
}
//Now list the array as it currently is after sorting
puts("\nHere is the list after the sort:");
for (ctr = 0; ctr <10; ctr++)
{
printf("%d\n", nums[ctr]);
}
}
else if (choice = 'N')
{
puts("Have a good day!");
}
return 0;
}
%c
format specifier with char
variable to read one character.char
变量的%c
格式说明符来读取一个字符。&
operator to get a pointer to a variable for scanf()
.&
运算符来获取指向scanf()
变量的指针。=
is an assignment operator in C. You should use ==
operator to check equality. =
是 C 中的赋值运算符。您应该使用==
运算符来检查相等性。 /* remove choice from int variable declaration */
char choice; /* use char */
puts(("(Reply Y or N): "));
scanf(" %c", &choice); /* use %c and & */
choice = toupper(choice);
if (choice == 'Y') /* use == */
{
//Now sort the array
/* omit */
}
else if (choice == 'N') /* use == */
{
puts("Have a good day!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.