Simple arrays

This is a discussion on Simple arrays within the C Programming forums, part of the General Programming Boards category; Hi guys I want to make a program where i get a user to enter 10 double numbers then it ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    Simple arrays

    Hi guys I want to make a program where i get a user to enter 10 double numbers then it gets stored in a array. then the numbers are displayed back to the user. Im just wondering if i would use the standard scanf and printf commands or is there something more useful?

    Code:
    #include <stdio.h>
    int main(void)
    
    {
    
       double numbers[10];     
    
    
    }
    I know its easy but im kind of new to programming so im having some difficulties understanding. any help would be great.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What could possibly be more useful than scanf?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    well i thought i was reading something earlier about read_array and show_array which is supposed to be used with arrays?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    and would I have to loop the scanf?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Those were user created functions. In C, it's always a good idea to break up a program, into smaller parts (functions), for input, processing, output.

    I wouldn't worry about it yet, but later, you'll want to tackle multiple functions, etc., as well.

    You'll want to use scanf("%lf", &yourDoubleVariableArray[i]), for input, inside a for loop.

    In pseudo code:
    Code:
    for 0 to 10
      scanf() as shown above
      print the number
      //anything else you want to do
    end for

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    Thanks Adak

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    so would this function correctly? sorry im not home on my comeputer with Dev c++
    Code:
    #include <stdio.h>
    int main(void)
    
    {
       double numbers[10];     
    
      for (0 to 10)
      scanf("%lf", &numbers[i]);
      printf("\n" &numbers)
      end for
    
    
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to look up the for() function in your C documentation... the way you got it ain't never gonna work. That's why adak labeled it "pseudo code"... The plan was to give you the idea, but you have to write your own code.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    for is not a function.
    Typical idiom of for loop is something like:
    Code:
    for(i = 0; i < n ; i++) {
       // do something n times
    
    }
    C Programming Tutorial

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    ok i tried something here is my code:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      double numbers[10];
      int count = 10;
     
    
      printf("\nEnter the 10 numbers:\n");
      int i;
    
      for(i = 0; i < count; i ++)
      {
        printf("%2d> ",i+1);
        scanf("%lf", &numbers[i]);
        }
     printf("%lf> ", &numbers[10]);
     
      
      	fflush(stdin);
    	getchar();
    
      return 0;
    }
    But now I am having some trouble displaying the numbers stored in the array after.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Code:
     printf("%lf> ", &numbers[10]);
    
      fflush(stdin);
    You need to know that printf and scanf work differently and accomplish separate things.

    http://cboard.cprogramming.com/c-pro...-tutorial.html

    Also, fflush(); only is guaranteed to work on output streams. If you want to "flush" stdin or an input stream you are expected to read everything. (Just as flushing an output stream writes everything.)

  12. #12
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    when your array is of size n, it has offset from 0 to n-1
    you are printinting array[n] which has garbage data.
    %d is used for integers. Double is one kind of floating value.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    While I can agree with what you said guarav, I don't think that is related to the current problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Simple question about arrays
    By tima in forum C Programming
    Replies: 3
    Last Post: 05-21-2007, 09:24 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21