Solved

Array initialisation with a loop

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

Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 137
cURL: stopping a http transaction before it's finished 3 116
negation in C function 14 144
How to learn Linux? 10 44
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…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

863 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

24 Experts available now in Live!

Get 1:1 Help Now