Thread: I have some trouble in the learning process, Arrays & Functions.

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Post I have some trouble in the learning process, Arrays & Functions.

    Hello guys,
    I'm learning about functions and i have some trouble with my code.
    I'm trying to write a program that calculates the average of numbers. the first stage is to ask the user how many numbers he wants to calculate, and than scan the numbers to an int type array.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void ScanInput(int numCount){
      int storage[numCount];
      for (int i=0; i < numCount; i++) {
        scanf ("%d ", &storage);
      }
      printf("%d\n", &storage[0]);
    }
    
    void main(){
      int numCount;
      printf ("How many numbers do you want to calculate? ");
      scanf ("%d ", &numCount);
      ScanInput(numCount);
    }
    In the function i used printf just to see that everything is fine with the scaning, but i'm getting strange output, I build and run the program, scaning the first number lets say its 12, the output is "1176217296"... i can't see the problem, why its not printing 12 instead of the 1176...

    Thank you for your time guys!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf ("%d ", &storage);
    You need to subscript your array to store values in it
    scanf ("%d ", &storage[i]);


    > printf("%d\n", &storage[0]);
    You also need a loop to print each element of the array as well.
    You can't just point at the first element, and expect the whole array to be printed.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    Thank you for the reply, but i still not getting the output that i want. I changed the code like this:
    Code:
    void ScanInput(int numCount){
      int i;
      int storage[i];
      for (i=0; i < numCount; i++) {
        scanf ("%d ", &storage[i]);
      }
      for (i=0; i<numCount; i++) {
        printf ("%d \n", &storage[i]);
      }
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is problematic because i has not been given an initial value:
    Code:
    int i;
    int storage[i];
    You probably wanted to write:
    Code:
    int storage[numCount];
    since your loops depend on numCount in a way that makes it seem like the number of elements in the array.

    Note that this relies on having a compiler that supports variable length arrays. The feature became standard in C99, but became optional in C11.

    Also, you should check the return value of scanf. You should also consider that the number of inputs may be less than the number of elements in the array, so you should keep track of this and then use this number to control the printing loop.
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf ("%d \n", &storage[i]);
    This.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any good sources on learning deployment process?
    By camel-man in forum Tech Board
    Replies: 0
    Last Post: 01-21-2016, 03:17 PM
  2. jUST LEARNING FUNCTIONS
    By Therry in forum C++ Programming
    Replies: 9
    Last Post: 04-09-2012, 11:40 AM
  3. Replies: 23
    Last Post: 01-19-2011, 02:24 PM
  4. Just learning C++ and am having trouble
    By Timbo8 in forum C++ Programming
    Replies: 10
    Last Post: 01-01-2007, 07:37 PM
  5. Learning arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-09-2002, 10:27 PM

Tags for this Thread