Solved

Array initialisation with a loop

Posted on 1998-12-11
5
203 Views
Last Modified: 2010-05-18
How is it possible to initialise an int type array using a loop. I tried the following, but it dosnt display the whole array, only the last element value.

#include<stdio.h>

int a[10];
int i, x;

main()
{

for(i=0; i<10; i++)
  {
  a[i];
  }

printf("array contents: %d", a[i]);

return 0;

}
0
Comment
Question by:bydysawd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
marcjb earned 50 total points
ID: 1255049
/* I added an initialization value, and then used another for loop to display the whole array. Hope this helps */

    #include<stdio.h>

    int a[10];
    int i, x;

    main()
    {

    for(i=0; i<10; i++)
      {
      a[i] = 5;
      }
 
    for(i=0; i<10; ++i)
      {
      printf("array contents: %d", a[i]);
      }
    return 0;

    }

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1255050
Damn, marcjb types faster

What do you want to initialize it to, each array item to the loop index then :-



     for(i=0; i<10; i++)
       {
       a[i] = i ; /* set the i'th element of a[], that is a[i], to i */
       }

The reason that "a[i];" compiles is that it is valid C, but does nothing.  You are probably aware that in C that it is possible to ignore the return values of functions, example :-

x = somefunc() ;
or
somefunc() ;

are both valid.

This rule also applies to expressions :-

e.g. these are both valid
x = 3 * 5 ;
3 * 5 ;

the second calculates 3*5 but then throws away the result.  Simiarly your code gets the pre-existing value of a[i], but then does nothing.


The next problem is the printf in your program.  At the end of the loop (the first }) i is 10.  Therefore your printf prints the item 10 of a, only.  As a only has items 0 to 9, this could potentially cause a crash, but also is definitely not what you want, therefore

either



     for(i=0; i<10; i++)
       {
       a[i] = i ;
       }

    for (x=0; x<10;x++)
    {
      printf( "Item %d of array is %d", x, a[x] ) ;
    }


or



     for(i=0; i<10; i++)
       {
       a[i] = i ;
      printf( "Item %d of array is %d", i, a[i] ) ;
       }

0
 

Author Comment

by:bydysawd
ID: 1255051
Thanks for your prompt reply.

Your programs works, however I wanted each element in the array to increment by one, and then the 'stdout' to show each array content:

array 1: 0
array2: 1
etc

Sorry I did not state the question clearer..but thanks a lot !
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1255052
The bit under "either" in my comment is that you need.

Array indexes always start from 0 in C by the way
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1255053
Answers2000's suggestion of:

for(i=0; i<10; i++)
 {
 a[i] = i ;
 }

in the initialization of the array will place the values
a[0] = 0, a[1] = 1, etc...  into the array.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question