ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Thanks alot !
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int k,i,j,number[20],number2[999]={0},lowerlimit,upperlimit;
bool exists;
printf("Type in 5 integers: "); // lägg in 20 tal
for(j=1;j<=5;j++) // loopa igenom dessa 20 tal
{
scanf("%d",&number[j]); // lägg in dessa 20 tal i "tal"
}
while(!(lowerlimit==0)) // sålänge undre gränsen inte är 0
{
printf("\n");
printf("Lower limit: "); // lägg in gränserna
scanf("%d",&lowerlimit);
printf("Upper limit: ");
scanf("%d",&upperlimit);
printf("\n");
for(i=1;i<=5;i++) // loopa 20 tal
{
if(number[i]>=lowerlimit && number[i]<=upperlimit) // om något utav talen är större än
{ // undre gränsen och mindre än övre så skall dem läggas i "tal2"
number2[i]=number[i];
}
}
for(j=lowerlimit;j<=upperlimit;j++) // loopa talen mellan undre och övre gränsen
{
exists=false;
for(k=1;k<=5;k++) // loopa dem en efter en
{
if(number2[k]==number[j]) // om tal2 inte finns med i tal så printa tal
{
exists=true;
}
}
if(exists!=true)
{
printf("%d,",j);
}
}
}
getch();
}
You didn't tell us what the problem was with the program, but I see one by inspecting the code.
You have a while loop governed by this code:
Code:
while(!(lowerlimit==0))
{
/* stuff inside the loop */
}
That code performs the test at the beginning of each loop. But you don't initialize lowerlimit before you come to this loop. What if it's zero before the first time you come to this loop? Then the while statement will say "whoa! lowerlimit is zero", and you'll never execute the content of the loop.
One would think that the following would be an improvement. Note that I've left out the initial "while" part entirely:
Code:
do
{
/* stuff inside the loop */
} while(!lowerlimit==0));
But that won't work either. Think about what you want your program to do. You want it to ask for a lower limit, and if the answer is zero, then you don't want to ask for an upper limit. So there must be a test between the two. Try something like this:
Code:
for(;;) /* infinite loop (sorry, you'll have to translate this into Swedish) */
{
printf("\n");
printf("Lower limit: "); // lägg in gränserna
scanf("%d",&lowerlimit);
if(lowerlimit==0)
{
break;
}
printf("Upper limit: ");
scanf("%d",&upperlimit);
printf("\n");
/* The rest of the loop goes here. */
}
There's another problem with your code that you probably haven't considered.
For a good program, avoid scanf(). Just for fun, I tried the following simplified version of your program:
Code:
#include <stdio.h>
int main(void)
{
int lowerlimit;
int upperlimit;
for(;;)
{
printf("\n");
printf("Lower limit: ");
scanf("%d",&lowerlimit);
if(lowerlimit==0)
{
break;
}
printf("Upper limit: ");
scanf("%d",&upperlimit);
printf("\n");
printf("The lower limit is %d and the upper limit is %d\n",
lowerlimit,
upperlimit
);
}
return 0;
} /* main() */
It seems to work fine. But try entering this data when it asks for a lower limit:
Code:
3 5 7
The result is rather weird, no? Then try entering this data when it asks for a lower limit:
Code:
3x5
I don't know what that will do for you, but on my system, entering that for a lower limit in the program I've posted above threw the program into an infinite loop.
Use fgets() to read the answer into a char array. (Of course, if you use a char pointer, make sure you use malloc() to make sure the pointer is actually pointing to memory, and always check the result of malloc() to make sure it's not NULL.) Then use sscanf() to decode the data. Use the end pointer parameter, and make sure it's pointing to a line feed.
And yeah I threw in a break there to get it to shut off before I could insert a upper value.
Problem is though I won't get any reasonable values at all something is wrong with my loops cause I'm getting results that looks like the loops are spinning a million(yes childish exaggeration) times more than they should.
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..
--------------------------------------------------------------------------
This seems like CS101 homework, so I won't just spell out the answer.
However, I have a few tips:
- if you make an array numbers[20], then you set aside enough space for 20 items. To get at these items, you simply index numbers[index]. However, indexing starts at 0, not at 1. Therefor, indexing from 1 to 5, while it will work now, won't work if you try to go from 1 to 20. Rather, it may, but the results are undefined.
- You probably want to do this as a do { } while(), as was suggested earlier. Actually, the better way to do this would be with a for(0-19) and just prompt each time for input, do your comparison, and then print out at the end.
- scanf is ok to use for homework.
- If you really want a tricky, but cool way of both impressing your teacher and getting the output you want, look at qsort as a way of reordering your array so that you always have the numbers in order (and therefore, you don't need to traverse the list each time).
Of course there is always the 'cheat' option if there is only 1 occurence of each num and they are guaranteed to be in a reasonable eg range eg 1-20. Init all array elements to zero, then just insert them in the matching array slot, ie arr[20]=20, arr[5] = 5 etc.
Effectively sorted without doing an explicit sort...
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101
Rep:
I see you have the library "conio.h", it might be irrelevant to this thread, but I do need this file. I have searched a lot to find conio.h for Linux but to no avail!
would you please either post it here(!) or send it to me.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.