Thread: arrays

  1. #16
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    I'm not sure if it's a good idea to declare variables inside of the loops
    its not. i think i heard someone say it only works under c99, something alot of compilers have problems with.

  2. #17
    Registered User
    Join Date
    Jul 2003
    Posts
    21
    This givese me the correct values while p = 0, but for p = 1, and p=2, they are all messed up. whats wrong with this? i fixed my variables, they are no longer declared in the loops

    Code:
    for (p=0; p<Row; p++)
    {   
    
      for (q=0; q<Column; q++)  
         {
         fSum += faArray[p][q];
         fAvg = fSum/7.0;
         }
         printf("Average Temperature for City %i: %10.2f\n", p, fAvg);
         
    }

  3. #18
    Registered User
    Join Date
    Jul 2003
    Posts
    21

    nevermind!

    thanks for the help guys, i figured out my problem. i needed to reset fSum to zero in my first nested loop, otherwise my numbers kept growing and not resetting. Thanks for the help, feels so good to be done . But it also feels good figuring it out, and I wanna keep programming!

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Originally posted by Hammer
    The method used in the code in the post above is valid, as ArraySize is a true constant.

    This is where you were coming from I guess, this shows legal and illegal use:
    No, I think I confused it with
    const int NOT_A_CONSTANT = 123;
    not being a true constant in C (according to my book).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by mart_man00
    I'm not sure if it's a good idea to declare variables inside of the loops
    its not. i think i heard someone say it only works under c99, something alot of compilers have problems with.
    Actually it works fine. A loop is in itself a block, and variables can be define at the beginning of blocks. At least as far back as 1990, Turbo C++ ver 1
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #21
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Well, it looks like the vars declared inside of the loop are local to it, so they can't be used outside, unless passed to function, etc..
    $ENV: FreeBSD, gcc, emacs

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by cc0d3r
    Well, it looks like the vars declared inside of the loop are local to it, so they can't be used outside, unless passed to function, etc..
    Of course. They're inclosed in a scope block. Anything defined in a new scope is only visible to said scope:
    Code:
    {  /*scope1*/
        int a=1;
        printf("%d\n",a);
        {  /*scope2*/
            int a=2;
            printf("%d\n",a);
            {  /*scope3*/
                int a=3;
                printf("%d\n",a);
                {  /*scope4*/
                    int a=4;
                    printf("%d\n",a);
                }  /* scope4 ends */
                printf("%d\n",a);
            }  /* scope3 ends */
            printf("%d\n",a);
        }  /* scope2 ends */
        printf("%d\n",a);
    }  /* scope1 ends */
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Jul 2003
    Posts
    21

    splitting an array into 2 arrays

    In one program I am working on, I ask the user to input an array. I then use that array into a function which creates 2 separate arrays, one with numbers divisible by 3, and the other with all other numbers. I know what I want to do, but I have no idea how to make these 2 arrays. Here is what I have, and I know it is messed up.

    Code:
    #include <stdio.h>
    
    int i, j, k;
    int iaArray2 [];
    int iaArray3 [];
    
    void iaModifyArray(int iSize, int iaArray[])
    {
       
       
       
        
        for (i=0; i<iSize; i++)
        {
             if (iaArray[i] % 3  == 0)
             
             for (j=0; j<iSize; j++)
                {
                int iaArray2 [j];                   //here is what i'm stuck on!
                printf("%f"  , iaArray2[j]);   //how do i assign these
                                                           //numbers to this array2??
                }
             
             else
             
             for (k=0; k<iSize; k++)       
                {
                int iaArray3 [k];                     //here also
                printf("%f"  , iaArray3[k]);
                }
              
        }
        
       }

  9. #24
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    While you scan each number from the input, you check if it is divisable by 3. If it is indeed, you put this value to array1, and you insert 1 to your index_a. Otherwise you put it in array2, and you also insert 1 to your index_b
    Loading.....
    ( Trying to be a good C Programmer )

  10. #25
    Registered User
    Join Date
    Jul 2003
    Posts
    21
    thats what i don't understand how to do. i don't know how to assign numbers from one array to another array. i understand what i want to do, but i just can't figure out how to implement it.

  11. #26
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >i don't know how to assign numbers from one array to another array.
    First of all, i didn't tell you to assign numbers from one array to another array.
    Second, array elements acts like "real" variables. That means that array[b] (thios is an array element wich is infact a mere variable) can get the value of array[c] or array2[b]. Whatever you know on mere variables apply it to array elements
    Loading.....
    ( Trying to be a good C Programmer )

  12. #27
    Registered User
    Join Date
    Jul 2003
    Posts
    21
    I am getting a bug somewhere. The debugger doesn't find it but my program isn't running right. It does the first part asking for the arrays, is my problem somewhere in this function? I'm getting an error while running the program that says "the memory can not be 'written'" It could be that I am using the printf statements wrong?? I don't know.

    Code:
    void iaModifyArray(int iSize, int iaArray[], int iaThrees[], int iaNotthrees[])
    {
       
        for (i=0; i<iSize; i++)
        {
             if (iaArray[i] % 3  == 0)
               {
                iaThrees[j++] = iaArray[i]; 
                printf("%i ", iaThrees[j]);      
             }
             else
               {
               iaNotthrees[k++] = iaArray[i];
               printf("%i ", iaNotthrees[k]);
           }
       }
       
       }

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You are incrementing j andk and then you try to access the element j/k. Since you already incremented it, chances are that you read past the limit.
    void iaModifyArray(int iSize, int iaArray[], int iaThrees[], int iaNotthrees[])
    {
    for (i=0; i<iSize; i++)
    {
    if (iaArray[i] % 3 == 0)
    {
    iaThrees[j] = iaArray[i];
    printf("%i ", iaThrees[j]);
    ++j;
    }
    else
    {
    iaNotthrees[k] = iaArray[i];
    printf("%i ", iaNotthrees[k]);
    ++k;
    }
    }
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #29
    Registered User
    Join Date
    Jul 2003
    Posts
    21
    This code still generates that error about memory. Is there a problem with my variables or something that could be causing this? In my program I've used the two functions as header files (its the assignment), but I've pasted the headers with the program so you can see it all. If something jumps out at you as retarded, please tell me

    Code:
    #include <stdio.h>
    #include "header1.h"
    #include "header2.h"
    
    
    int GetArray();
    void iaModifyArray();
    
    
    int GetArray(int iaArray[])    //Gets array from user
    {    
    
         int iArraySize, iSize, i;
         printf("Enter size of array: ");
         scanf("%i", &iArraySize);
         
         iSize = iArraySize;
         
         for (i = 0; i < iSize; i++)
            {
             printf("Enter value %i: ", i);
             scanf("%i", &iaArray[i]);
             
             }
        
        return(iaArray[iSize]);
    
    }
    
    
    void iaModifyArray(int iSize, int iaArray[], int iaThrees[], int iaNotthrees[])               //creates 2 seperate arrays
    {
        int i, j, k;
        for (i=0; i<iSize; i++)
       {
          if (iaArray[i] % 3 == 0)
         {
            iaThrees[j] = iaArray[i]; 
            printf("%i ", iaThrees[j]);
           ++j;
         }
         else
        { 
           iaNotthrees[k] = iaArray[i];
           printf("%i ", iaNotthrees[k]);
          ++k;
         }
       }
    }
    
    int main(void)
    {
    
    	GetArray();
    	iaModifyArray();
    	return 0;
    }

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM