Thread: A simple program I can't solve

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    3

    A simple program I can't solve

    Hello, I am a student in a technical university, we are only studying some basic programming in C language. I have to make this program and I can't find a way to do it. The program is like this: You are supposed to enter a set of numbers (however much you want), when you are done with your sequence of number, you press 0 and then the program prints out the numbers you have entered from interval <2:5>. Now if you are thinking of somekind of a complex code, DON'T. It should be a very basic code, I'm guessing with the use of (while == 0).

    Any help would be very appreciated.
    (PS sorry for my lame English)

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Have you made an attempt to solve the problem? If so, post your code along with any problems you are having.
    Double Helix STL

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A development process
    Break the problem down into steps
    - enter 1 number and print it out
    - enter 10 numbers and print them out
    - enter 10 numbers, and "prints out the numbers you have entered from interval <2:5>"
    - enter numbers until 0 is entered, then ...

    See?

    Now, how many of those intermediate steps can you manage yourself?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Hello, I'm really thankful for your reply.

    I can see your point, but what I don't understand is how to make an infinite array of numbers, or if there is a way of doing it without arrays. For example, if I had to use an array of 30 numbers, I would just use the for command and it would look like this

    Code:
    #include<stdio.h>
    
    
    int main()
    {
      int j, k[30];
      for (j=1; j<=30; j++)
          {
          printf("Enter a number: ");
          scanf("%d", &k[j]);
          }
      printf("Numbers in interval <2;5> are {%d, %d, %d, %d}", k[2], k[3], k[4], k[5]);
      
      return 0;
    }
    But I really have no clue how to print out an exact number with a while command..

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The problem statement does NOT require an infinite list of numbers.
    It just requires a list of 4 numbers along with the current value being entered.
    Also, requires a counter variable to count the numbers that have been entered.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Just a note:

    Code:
    int j, k[30];
    for (j=1; j<=30; j++)
    Arrays start at zero and end at n-1.

  7. #7
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Quote Originally Posted by stahta01 View Post
    The problem statement does NOT require an infinite list of numbers.
    It just requires a list of 4 numbers along with the current value being entered.
    Also, requires a counter variable to count the numbers that have been entered.

    Tim S.
    THANK YOU SO MUCH. Your reply was most helpful, it totally enlightened me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need to solve simple c problem
    By raihan004 in forum C Programming
    Replies: 10
    Last Post: 09-16-2012, 12:44 PM
  2. switch statement. simple to solve but i dunno whats wrong.
    By ingeniousreader in forum C Programming
    Replies: 2
    Last Post: 05-17-2012, 09:43 AM
  3. Need help to solve this program
    By kitymarine in forum C Programming
    Replies: 2
    Last Post: 06-20-2011, 11:10 PM
  4. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 06:59 PM
  5. simple i/o error that I cannot solve
    By CodeMonkey in forum C++ Programming
    Replies: 10
    Last Post: 12-17-2006, 04:07 PM

Tags for this Thread