LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2007, 06:55 AM   #1
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Rep: Reputation: 15
Some noob C problems


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.

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();
}
 
Old 09-24-2007, 07:08 AM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Great, i see you forgot to mention what was wrong with it, or did i miss that part?
 
Old 09-24-2007, 07:27 AM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.

Hope this helps.
 
Old 09-24-2007, 07:48 AM   #4
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
Thanks for the replies

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..
--------------------------------------------------------------------------
 
Old 09-24-2007, 01:13 PM   #5
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
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).


-Aaron
 
Old 09-24-2007, 09:22 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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...
 
Old 10-21-2007, 06:56 PM   #7
nmansour
Member
 
Registered: Sep 2006
Location: Chicago
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101

Rep: Reputation: 15
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.

Noha
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Startup Problems with 10.1 (noob) cueman SUSE / openSUSE 1 10-04-2006 12:30 AM
Noob having strange problems with C ninjabob7 Programming 3 06-23-2006 05:51 PM
NOOB having kernel problems megadsonic SUSE / openSUSE 6 03-16-2006 08:03 AM
Noob Problems installing VanHendrix Debian 5 10-24-2005 11:00 AM
A few problems here as an extreme noob Raggit Linux - Newbie 4 03-07-2004 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration