?
Solved

Array initialisation with a loop

Posted on 1998-12-11
5
Medium Priority
?
205 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 150 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

Industry Leaders: 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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

765 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