array help

This is a discussion on array help within the C Programming forums, part of the General Programming Boards category; im writing a program that will... i am *trying to write a program that will store words and integers using ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    array help

    im writing a program that will... i am *trying to write a program that will store words and integers using arrays but i dont really understand how to write one. can anyone try and explain how to write an array?
    more specifically, i am writing a food bank program that will store the food type donated and the amount. im not asking anyone to write the program, just an explanation of how arrays work and how to write one.
    thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If this is for real use, you should be using a database (or at least a spreadsheet) program. There are some free one's around, so check it out with Google.

    If this is for an assignment, you need to review your class notes, read the chapter on arrays in your text book, and use Google to hustle up some C tutorials, which would of course, include arrays.

    We can't cover the topic of array's as fully, or as coherently, as a chapter in a book. We have no editors.

    Please return when you have a problem with your program - we're set up (and good at), helping with that kind of situation. No book can compete with us in that specialty.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    no its an assignment for a c programming class.

    i dont have a text book otherwise i would have done that.

    if someone could just post a simple program that will read in a word an store it in an array that would be great. but as simple as it gets... nothing but read in the characters and how to declare it an such. i can figure it out from there. thanks for the help.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    whats really confusing me is the index part.

    what does this do?
    Code:
    while (word[index] != '\0')

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    any one?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ominub View Post
    no its an assignment for a c programming class.

    i dont have a text book otherwise i would have done that.

    if someone could just post a simple program that will read in a word an store it in an array that would be great. but as simple as it gets... nothing but read in the characters and how to declare it an such. i can figure it out from there. thanks for the help.
    No text book? Geez!

    I can't test this right now because I have no access to my compiler atm, but this will be close:

    Code:
    //create two integer variable
    int i, fetched;
    
    /* create an array of type char, with 50 rows (names), with each row having 25 columns (or letters) for it.
    
    Fill each row, (one curled brace), and each column in each row, (another curled brace), 
    with the special char that indicates an end of string marker, in C: '\0'. Note the *single* 
    quote marks around it - indicating it is just *one* char, not two or more char's.
    */
    char names[50][25] = { { '\0' } };
    
    FILE* in;  //create a file pointer, note the all caps - a rarity in C
    
    
    */Open a file named filename, for reading as a text file and assign the pointer it returns, 
    to our FILE pointer, in. Note that "in" is just a name, nothing special about it.
    
    
    
    if((in = fopen("filename", "rt")) == NULL)  {
       printf("File didn't open, program terminating");
       return 1;   //return of zero means "normal", anything else (like 1), means abnormal exit
                        //sometimes this convention is swapped, on different operating systems, etc.
    }
    
    //If control gets here, the file is open, so let's read in the data
    
    
    /* fetched is the number of items that fscanf will be returning. When it reaches the end of the file, 
    it will return 0, and the for loop will end. We have to start the loop with fetched being assigned 1, 
    or the loop will never start at all.
    
    Even if there are more than 50 names, stop at 50, to prevent our program from crashing.
    
    
    for(i = 0, fetched = 1; i < 50 && fetched; i++)  {
       fetched = fscanf(in, "%s", &names[i]); 
    }
      
    /* now:
    names[0] will be our first name
    names[1] will be out second name
    
    names[0][0] will be the first letter in the first name
    names[1][0] will be the first letter in the second name, etc.
    
    Note: Our 50th name will be in names[49]
    
    */
    I don't like posting code that I have not had a chance to compile or look up in a book. This is the best I can do without that help.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Look! An array tutorial! And another!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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