C program sort array then get user command

Hi all, I have a c programming question I am stuck on.
The question first asks to write a program that takes multiple values and stores the numbers in an array. The array should not be more than 10. Then sort the array in ascending order.

once the array is sorted, the next job is to get user command:

user command: i for insertion, q for quit

typing 'i' prompts for one additional number
typing 'q' quits program

this is the problem i cant solve, i have figured out the whole program accept how to insert a user command to quit or continue after sorting the first array.

if the user types 'i' the program gets a number input from the user and insert one additional number in the correct (ascending order) position of the array.

i tried inserting this after the first array is sorted but did not work:

1
2
3
4
5
6
7
printf("Enter 'q' to quit or 'i' to continue\n");
  scanf_s("%c", &command);
	  while(command!='q' || command!='Q')
	  {
		  while((command='i')|| (command='I'))
		  {


i also tried an if statement on the quit or insert and couldnt get it to work?

Any help will be greatly appreciated, I've been stuck on this for a few days.

here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
#include<stdio.h>
int main()
{
	
	int s,x,j,temp,a[20]; 

  printf("Please enter the array size of no greater than 10:\n ");//Prompt
  scanf_s("%d",&s);//Obtain array size from user
  while(s>10)
  { printf("Please enter the array size, LESS than 10\n");//Prompt
  scanf_s("%d",&s);}//Re-enter array size less than 10

  printf("Please enter %d elements, to sort into ascending order: \n",s);//Prompt
  for(x=0;x<s;x++)
      scanf_s("%d",&a[x]);//Obtain array elements

  for(x=0;x<s;x++){
      for(j=x+1;j<s;j++){
           if(a[x]>a[j]){
               temp=a[x];
              a[x]=a[j];
              a[j]=temp;
           }//Sort array into ascending order
      }
  }

  printf("the strings are: \n");
  for(x=0;x<s;x++)
  { printf(" %d\n",a[x]);}//Print array in ascending order



//Problem section -
  //where i need to prompt user "Enter 'q' to quit or 'i' to continue


			  printf("Enter a number to insert into the array in its corresponding position:\n");//Prompt
			  for(x=s;x<(s+1);x++)
			  {
				  scanf_s("%d",&a[x]);
			  }//Obtain 1 additional number and insert into array
				  for(x=0;x<(s+1);x++)
				  {
					  for(j=x+1;j<(s+1);j++)
					  {
						  if(a[x]>a[j])
						  {
							  temp=a[x];
							  a[x]=a[j];
							  a[j]=temp;
						  }//Sort new array into ascending order
					  }
				  }

		 

			  printf("The new string is: \n");
			  for(x=0;x<(s+1);x++)
			  
			  {
				  printf(" %d\n",a[x]);//Print new array into ascending order
			  
			  }
		  
		  
	  
  return 0;
  
}
Last edited on
Topic archived. No new replies allowed.