Thread: help with arrays

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    help with arrays

    hey guys,
    i'm a newbie and need some assistance to begin my assignment.
    Well i need to prompt the user with a category(i.e. cd's) and ask
    three questions "What is the amount in stock, the average price, and how sell percentage". Basically i need to do this 10 times for ten different categories,ask the same three question, and output all info,but must be store in 3 arrays. Any suggestions?

    thanks,
    sal

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I dont see where 3 arrays is necessary. Create a structure which holds all the info that you need. Then create an array of 10 of those structures, then fill the array.

    Here is what the stucture could look like:
    Code:
    struct Information
    {
        char szCategory[64];
        int nStock;
        float fPrice;
        float fSellPercentage;
    };

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    if he is learning how to use arrays, then i don't think he would have learnt to use structs yet.
    sal817: don't you have some kind of program development procedure to follow? what you are asking help for is quite simple. Try to write a solution and show us what you some up with first

  4. #4
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    basically u need to make 3 arrays

    like
    Code:
    int myArray[10];
    
    for (int x=0; x<10;x++)
      myArray[x] = 99;
    this is going to fill the array with number 99
    so basically

    myArray[0]=99
    myArray[1]=99
    myArray[2]=99
    myArray[3]=99
    ...
    myArray[9]=99
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    help with array/p2

    Thanks sand_man,
    I'll try to come up with code and repost. One more question, how do i prompt the user to select a categorey of 10 and then go to the array to store info and after info is store return to menu and select a different category and do the same process again.

  6. #6
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    you could use "cases" or simply use a do while loop.
    example:

    do {

    ...some code

    }while (until this happens);


    Ask the user for the input then create an array. ex:
    Code:
    cout << "Enter array size: ";
    cin >> arraySize;
    int myArray[arraySize];
    
    do {
    
    ... some code
    
    }while();
    Last edited by dayknight; 09-17-2004 at 12:25 PM.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by dayknight
    Code:
    cout << "Enter array size: ";
    cin >> arraySize;
    int myArray[arraySize];
    
    do {
    
    ... some code
    
    }while();
    Please no...couple of things wrong with that:

    1) Variable definitions should come before any code.
    2) That's C++ code, not C.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>how do i prompt the user to select a categorey of 10
    Use scanf and printf

    >>then go to the array to store info
    Store info in array[category_index]

    >>return to menu and select a different category
    As has been mentioned, stick the menu->store code in a loop.
    Code:
    while(!done)
    {
       //Display menu and get input
       //Do your storing stuff
    }
    And to get it to break out of the loop properly, you'll have to juggle it around into some arrangement that quits the loop when the input says 'quit' or something.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int amount_in_stock[10];
        int avg_price[10];
        int sell_percentage[10];
        int i;
        
        
        for(i=0;i<10;i++)
        {
            scanf("%d",&amount_in_stock[i]);
            scanf("%d",&avg_price[i]);
            scanf("%d",&sell_percentage[i]);       
        }    
        return 0;
    }

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by sand_man
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int amount_in_stock[10];
        int avg_price[10];
        int sell_percentage[10];
        int i;
        
        
        for(i=0;i<10;i++)
        {
            scanf("%d",&amount_in_stock[i]);
            scanf("%d",&avg_price[i]);
            scanf("%d",&sell_percentage[i]);       
        }    
        return 0;
    }
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int stock[10];
      int avg_price[10];
      int percent_sold[10];
      int i;
    
      for ( i = 0; i < 10; i++ ) {
        if ( scanf ( "%d%d%d", &stock[i], &avg_price[i], &percent_sold[i] ) != 3 ) {
          /* Handle an input error */
        }
      }
    
      return 0;
    }
    Always check the return value from input functions. And in this case there's no good reason to split up the calls to scanf. By splitting the calls you can better diagnose errors, but at this level of programming it's better to just print an error message and bail than to make any real attempt at recovery.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM