Thread: While loop with array

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    26

    While loop with array

    Assuming that arr is an array containing 100 elements I need to write the equivalent while loop that corresponds to the following for loop.

    Code:
    for ( i=0, s=0.0;i<100;s=s+arr[ i ], i++) ;
    Here what I have done:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    int s;
    int i;
    
    s = 0.0;
    i = 0;
    
    while ( i<100 ){ 
    printf( "%d \n", i = i++ );
    s = s+arr[i];
    }
    
    return 0;
    }
    But how do i declare the array?

    Errors recieved :

    xc.c(14) : error C2065: 'arr' : undeclared identifier
    xc.c(14) : error C2109: subscript requires array or pointer type


    I doubt that I have to use define SIZE.

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How do you not declare it? You must have created something that you can manipulate in the first place. The first code just does not show where.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    int arr[100];
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    26

    Wink

    Thanks guys....problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Initializing a 3D array w/o a loop.
    By funkydude9 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2004, 11:20 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Character in Array to end for loop.
    By mattz in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 11:05 AM