Solved

Array initialisation with a loop

Posted on 1998-12-11
5
198 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now