Jump to content

Noob C question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Nousferatou

Nousferatou

    Newbie

  • Members
  • Pip
  • 7 posts
Hi!

The code is supposed to read in 20 integers(I changed it to 5 to begin with just to make the trial&error process smoother)

After 20 integers are stored in array "number" it should ask for a lower and upper limit. The numbers that are between these two values lets say
20 is lower and 40 is upper..then 21,22,23,24...39,40 should be the numbers in between, now what I want is for the program to display what numbers that DO NOT exist between these two limits.

What I mean is the numbers i typed in from the start..like if the limits are 16 to 19 and I only typed in 17 and all my other choices were either above or under the limits.
So it should display 16,18 and 19 as missing.
Like "Hey you got 17 right but 16,18 and 19 are missing!"
And the script should stop accepting new limits after you've typed in 0 as the lower limit.

The correct output of the program should be:
-------------------------------------------------------------------------
Type in 20 integers:
4 7 8 9 2 14 15 56 3 7 23 24 25 27 29 32 31 23 16 26

Upper limit: 14
Lower limit: 16

The number 14 up to 16 are all present.

Lower limit: 24
Upper limit: 32

Between 24 and 33 the following numbers are missing: 28, 30, 33.

Lower limit: 0

Game over..
--------------------------------------------------------------------------

I'v posted my code and it works fine if you just want to display what numbers that are between lowerlimit and upperlimit.
But I need to list whatever numbers that aren't present aswell :(

Could anyone help out here whatever I'm just messing my loops up, I'd really appreciate if you'd be thoroughal since I'm very new with C I'v been taking this course for 2 weeks.










#include <stdio.h>

#include <conio.h>

#include <stdlib.h>



int main(void)

{ 

    int i,allnumbers[20],lowerlimit,upperlimit;

    

    printf("Type in 20 integers: ");

    

    

    for(j=1;j<=20;j++)

    {

        scanf("%d",&allnumbers[j]);

    }

    

              while(!(lowerlimit==0))

              {

                    printf("\n");

                    printf("Lowerlimit: ");

                    scanf("%d",&lowerlimit);

                    if(lowerlimit==0)break;

                    printf("upperlimit: ");

                    scanf("%d",&upperlimit);

                    

              

                                           

                        for(i=1;i<=20;i++)

                        {      

                              if(allnumbers[i]>=lowerlimit && allnumbers[i]<=upperlimit)

                              {

                                       printf("%d ",allnumbers[i]);

                            

                              }     

                        }

              }

                               printf("Game over!");

getch();                

}

        

              

                         


#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
If you want to make a list of the numbers that aren't between the upper- and lower-limit, you should simple reverse the if-statement, you're using for listing the numbers that are between.
if(allnumbers[i]<lowerlimit && allnumbers[i]>upperlimit)


#3
Nousferatou

Nousferatou

    Newbie

  • Members
  • Pip
  • 7 posts

v0id said:

If you want to make a list of the numbers that aren't between the upper- and lower-limit, you should simple reverse the if-statement, you're using for listing the numbers that are between.

if(allnumbers[i]<lowerlimit && allnumbers[i]>upperlimit)


Nah I guess I was unclear I want to know how to print the numbers that are missing between the lower & upper limit.
Like if the limit was 14 to 18 and i type in 20 integers and only one of them are between 14 and 18 lets say 15..
Now there's 14,16,17 and 18 missing and I'd like my program to print that out like the "correct output" above.

thanks for the reply !

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Oh, in that way.
Take a look at this code:
#include <stdio.h>
#include <stdlib.h>

int main()
{ 
    int i, j;
	int allnumbers[20];
	int lowerlimit;
	int upperlimit;
	int result;
    
    printf("Type in 20 integers: ");
    for(i = 0; i < 20; i++)
        scanf("%d", &allnumbers[i]);
    
    while(lowerlimit != 0)
    {
		printf("\n");
		
        printf("Lowerlimit: ");
        scanf("%d", &lowerlimit);
        
        if(lowerlimit == 0)
			break;
		
		printf("upperlimit: ");
        scanf("%d", &upperlimit);
        
        printf("\n");
        printf("These are the numbers between %d and %d, you wrote:\n ", lowerlimit, upperlimit);  
        for(i = 0; i < 20; i++)
            if(allnumbers[i] >= lowerlimit && allnumbers[i] <= upperlimit)
                printf("%d ", allnumbers[i]);
        
        printf("\n");
        printf("These are the numbers between %d and %d, you didn't wrote:\n ", lowerlimit, upperlimit);
		for(j = lowerlimit; j <= upperlimit; j++)
		{
			result = 0;
			for(i = 0; i < 20; i++)
			{
				if(allnumbers[i] >= lowerlimit && allnumbers[i] <= upperlimit)
					if(allnumbers[i] == j)
						result = 1;
			}
			
			if(result == 0)
				printf("%d ", j);
		}
	                
		printf("\n");
    }
    
    printf("\n");
    printf("Game over!\n");
                               
    return 0;
}
This is how my output looks like:
Type in 20 integers: 4 7 8 9 2 14 15 56 3 7 23 24 25 27 29 32 31 23 16 26

Lowerlimit: 14
upperlimit: 16

These are the numbers between 14 and 16, you wrote:
 14 15 16
These are the numbers between 14 and 16, you didn't wrote:


Lowerlimit: 1
upperlimit: 40

These are the numbers between 1 and 40, you wrote:
 4 7 8 9 2 14 15 3 7 23 24 25 27 29 32 31 23 16 26
These are the numbers between 1 and 40, you didn't wrote:
 1 5 6 10 11 12 13 17 18 19 20 21 22 28 30 33 34 35 36 37 38 39 40

Lowerlimit: 10
upperlimit: 30

These are the numbers between 10 and 30, you wrote:
 14 15 23 24 25 27 29 23 16 26
These are the numbers between 10 and 30, you didn't wrote:
 10 11 12 13 17 18 19 20 21 22 28 30

Lowerlimit: 0

Game over!



EDIT: For some reason the formatting got really screwed, but I hope you can read it anyway.

#5
Nousferatou

Nousferatou

    Newbie

  • Members
  • Pip
  • 7 posts
Alright thanks alot man!