What is wrong with the code below. The goal is to make a program that ask the us
ID: 3591389 • Letter: W
Question
What is wrong with the code below. The goal is to make a program that ask the user for the amount of numbers, then the user gives the numbers and it is sorted in three ways ascending descending and user entered. Its not working properly and I can't seem to get user entered to work at all. There is no error message. It compiles fine the problem is I can't seem to enter any information. It just prints
#include
int main()
{
int number[100];
int i, j, a, n;
char userInput;
printf("Please enter number of numbers: ");
scanf("%d", &n);
printf("Please enter %d numbers: ", n);
printf("Would you like your array to be sorted ascending, descending or normal? ");
printf("Type a for ascending, d for descending, or anything else for normal ");
scanf("%c", &userInput);
switch(userInput) {
case 'a':
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in descending order are given below ");
for (i = 0; i < n; ++i)
{
printf("%d ", number[i]);
}
break;
case 'd' :
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
/* sorting begins ... */
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in descending order are given below ");
for (i = 0; i < n; ++i)
{
printf("%d ", number[i]);
}
break;
}
return 0;
}
Explanation / Answer
The first basic thing "You have to include header file for io operations" i.e, STDIO header file.
The first line you left with #include.
It should be like, #include<stdio.h>
With out including this headerfile io operations won't work. Below is the working code of it... Please check it...
#include<stdio.h>
int main()
{
int number[100];
int i, j, a, n;
char userInput;
printf("Please enter number of numbers: ");
scanf("%d", &n);
printf("Would you like your array to be sorted ascending, descending or normal? ");
printf("Type a for ascending, d for descending, or anything else for normal: ");
scanf(" %c", &userInput);
switch(userInput) {
case 'a':
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i) {
for (j = i + 1; j < n; ++j) {
if (number[i] > number[j]) {
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in descending order are given below ");
for (i = 0; i < n; ++i) {
printf("%d ", number[i]);
}
break;
case 'd' :
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
/* sorting begins ... */
for (i = 0; i < n; ++i) {
for (j = i + 1; j < n; ++j) {
if (number[i] < number[j]) {
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in descending order are given below ");
for (i = 0; i < n; ++i) {
printf("%d ", number[i]);
}
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.