I have been editing my code for a while now where it will finally display the in
ID: 3591503 • Letter: I
Question
I have been editing my code for a while now where it will finally display the intro and counting correctly, but will not print the results.
The goal of the program is to ask the user how many elements they want read then they insert the elements. After that they are asked which way they want the array to be read, acending order, descending order, and standard order. The program will now loop infinitely and the results are never shown. What am I doing wrong?
#include <stdio.h>
int main()
{
int number[100];
int i, j, a, n;
char userInput;
printf("Enter the number of elements ");
scanf("%d", &n);
float array[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);
// read data
printf("Please enter data: ");
for (i = 0; i < n; i++)
{
printf("Enter entry #%d: ", i+1);
scanf("%f", &array[i]);
}
switch(userInput) {
//ascending
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 ascending order are given below ");
for (i = 0; i < n; ++i) {
printf("%d ", number[i]);
}
break;
//descending
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;
// standard
case 's' :
for(i=0; i<n; i++)
{
scanf("%d", number[i]);
}
printf(" Elements in array are: ");
for(i=0; i<n; i++)
{
printf("%d, ", number[i]);
}
break;
}
return 0;
}
Explanation / Answer
Here I have made some changes to the code and it is working well now. Along with the code I have provided the output also.
Code:
#include <stdio.h>
int main(){
int number[100];
int i, j, a, n;
char userInput;
printf("Enter the number of elements ");
scanf("%d", &n);
float array[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);
// read data
printf("Please enter data: ");
for (i = 0; i < n; i++){
printf("Enter entry #%d: ", i+1);
scanf("%d", &number[i]);
}
switch(userInput) {
//ascending
case 'a':
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 ascending order are given below ");
for (i = 0; i < n; ++i) {
printf("%d ", number[i]);
}
break;
//descending
case 'd' :
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;
// standard
case 's' :
printf(" Elements in array are: ");
for(i=0; i<n; i++){
printf("%d, ", number[i]);
}
break;
}
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.