Thread: Array problem. Can you find the problem with this?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    Array problem. Can you find the problem with this?

    Hi guys

    this is what I want the program to do:

    it's to save up to 100 given number>0 from keyboard in some Array. If the given number is smaller than 0, it should stop and give back all the numbers inserted up to then, but backwards. so it gives out all the numbers saved in the array from the last typed number to the first one.

    this is my idea which apparently doesn't work. and I can't see the reason..I'd be thankful for any hint


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        int i;
    
     int number [100]={};  //"number" is just a name for the array
    
    
       printf("give a number!\n");
    
    
        scanf("%d",&i);
    
         while(i>0){
    
     number[100] = i;    //saves the number in the array?
    
             getchar();
    
    
             if(i<0)
    
                break;
             }
    
    
      printf("%d",number[--i]);
    
                 putchar('\n');
    
        
         return EXIT_SUCCESS;
    }
    Last edited by Aitra; 01-01-2013 at 06:46 PM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    You need to do make your "i" variable in your while loop change every time you put a new number in it. Then, when you are trying to print your array out backwards, you need another similar while loop to accomplish this.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    You also need to initialize your "i" variable to a value of your choice before it starts testing its value in the while loop condition (i>o), but i is not defined at this point as anything meaningful.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by gratiafide View Post
    You also need to initialize your "i" variable to a value of your choice before it starts testing its value in the while loop condition (i>o), but i is not defined at this point as anything meaningful.
    I would say that it is being initialized with the scanf right before the loop
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok let's have a thorough look at this code

    Declare an array of 100 integers like this
    Code:
    int number [100];
    not like this
    Code:
    int number [100]={};
    .
    Now we have an empty array, ready to store our data

    This is a bad line of code.
    Code:
    number[100] = i;
    The indexing of the arrays start from zero and must stop at N-1. So, in our case, the first element is number[0] and the last number[99].
    As a result, if you say number[100], you are out of bounds. Segmetation fault or a bus error may occur!
    Also, what we want to do, is to read a number, test it (if it positive or zero) and then store in it an array...
    So, let's suppose you run this by hand, the procedure would something like this... (suppose that the test are ok and you go until the last number)
    • Read the first number, test it, save it in first position of the array (that is number[0])
    • Read the second number, test it, save it in 2nd position of the array (that is number[1])
    • Read the third number, test it, save it in third position of the array (that is number[2])
      ....
    • Read the 99th number, test it, save it in 99th position of the array (that is number[99])

    So, now you have to transform this procedure, into a loop that will do three jobs (read-test-store).

    Then after you do that, you have to print them. You are almost done
    Let's print 100 numbers, from the first to the last.
    Code:
    int i;
    for( i = 0 ; i < 100 ; i++)
    {
            printf("number[%d] = %d\n", i, number[i]);
    }
    You have to transform this to fit your needs

    // You don't need getchar ( I can explain, but I will leave it after you are done with this. Of course, you have to remind me about it, if you are interested
    EDIT:
    //Happy new year to all, with health first of all!
    Last edited by std10093; 01-01-2013 at 07:58 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    Ok let's have a thorough look at this code

    Declare an array of 100 integers like this
    Code:
    int number [100];
    not like this
    Code:
    int number [100]={};
    .
    Now we have an empty array, ready to store our data
    If you do want to zero initialise your array, then I suggest:
    Code:
    int number[100] = {0};
    as I recall something about the empty initialiser being non-standard in C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Quote Originally Posted by std10093 View Post
    I would say that it is being initialized with the scanf right before the loop
    Okay but then it instantly goes into an infinite loop....... he needs to make it meaningful by allowing the "i" variable to change in his loop, as your nice for loop shows.
    Last edited by gratiafide; 01-01-2013 at 11:35 PM.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    If you do want to zero initialise your array, then I suggest:
    Code:
    int number[100] = {0};
    as I recall something about the empty initialiser being non-standard in C.
    Indeed, I found that out today: It's non-standard in C, but standard in C++.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    @the "big" users : I really don't know about that. Can you give me a source? I can't find one...

    @gratiafide : The loop will do what ever you command it.
    You have to think about which is the maximum number your loop can be executed. You are going to read up to 100 numbers (supposing that all are zero or positive). You must not read more than that, because your array has room for 100 numbers, not more
    So, I would suggest you to use a for loop (like the one I used for printing) and inside the loop do all the work I described
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Nobody knows about the empty initialiser ?? :/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Elements with missing values will be initialized to 0:

    Code:
    int myArray[10] = { 1, 2 };
    initialize to 1,2,0,0,0...So this will initialize all elements to 0:

    Code:
    int myArray[10] = { 0 };
    all elements 0In C++, an empty initialization list will also initialize every element to 0:

    Code:
    int myArray[10] = {};
    all elements 0 in C++Remember that objects with static storage duration will initialize to 0 if no initializer is specified:

    Code:
    static int myArray[10];
    also...

    1)In C, NULL may or may not be the same thing as 0 (zero).
    You want to set the elements to zero, not to NULL. NULL is for pointers.

    2)it depends on where the array is allocated. If it is a global or static variable it will be initialized to zero by default. If it is a local variable you can write = {0} as mentioned above. That means that the first element will be set to zero and the rest of the elements will be treated as if they were static variables, which means they will also be set to zero by default.

    If you write programs that will run in a tough environment, you don't want to rely on the initialized values at all, but rather set them in runtime before the variable is used. That is in a for-loop, which is the slowest but safest way to do it.
    all elements 0And that "0" doesn't necessarily mean "all-bits-zero", so using the above is better and more portable than memset(). (Floating point values will be initialized to +0, pointers to null value, etc.)
    Last edited by Crossfire; 01-02-2013 at 02:53 PM.

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I appreciate the big post you wrote, but that didn't answer my question.

    If you right
    Code:
    int array[5];
    you don't write standard C code?


    //I am aware of the garbage values of course (and all the things you said)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    ahhhhh....the statement was about the right way to "initialise" the array, not declare it!!! lol

    so
    Code:
    int array[5];
    isnt the prob....

    Code:
    int array[5]={};
    was the question, and no, for C, that is not NOT standard....the null only is standard for C++

    Code:
    int array[5]={0};
    is the C standard to initialise the array with 0

    seriously though, are you being serious or just busting balls?

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I see Tnx

    I am was as serious as this guy (when asking)
    Array problem. Can you find the problem with this?-15746-serious-sam-first-encounter-windows-screenshot-game-logos-jpg
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  15. #15
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    I usually dont really show emotions, nor actually LOL, but i had to chuckle at that!!! Thank for making a crappy day feel less crappy for 45 seconds!

    btw, wonder if he actually figured out his prob, or if he is cross posting and making other people laugh at him!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't seem to find the problem, help.
    By JoeyJoe in forum C Programming
    Replies: 8
    Last Post: 03-04-2011, 02:03 PM
  2. help me to find the problem
    By behzad_shabani in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 01:17 AM
  3. pls help i cant find the problem :(((
    By condorx in forum C Programming
    Replies: 3
    Last Post: 11-07-2002, 09:05 AM
  4. Can't find problem
    By b-ref in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 05:39 AM
  5. can anyone find the problem in my code
    By ArseMan in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 09:02 PM