arrays

This is a discussion on arrays within the C Programming forums, part of the General Programming Boards category; how do i print out the values of an array as output? i can only figure out how to put ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    21

    arrays

    how do i print out the values of an array as output? i can only figure out how to put one value of an array as output, but not the whole string.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    If it is a string you can say:
    printf( "%s", string );
    or
    puts( string );

    If it is an array of chars or integers or floats or ... you just have to loop through all its elements and print each of its elements seperately one-by one.
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    21
    i need to pass my array into another function and modify it so that it prints out a modified array where every value is multiplied by 2. so far i have this. but i haven't started multiplying the values by 2 because i'm not even sure how to pass my array to a function. i'm trying to pass on the array like i was taught, but not sure if this is the right way to go about it.


    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    enum {ArraySize = 5};
    void GetArray()
    {
      
      
       float fA, fB, fC, fD, fE;
       
       int iIndex;
        float faArray[ArraySize]; 
        printf("Enter value 1: ");
        scanf("%f", &fA);
        printf("Enter value 2: ");
        scanf("%f", &fB);
        printf("Enter value 3: ");
        scanf("%f", &fC);
        printf("Enter value 4: ");
        scanf("%f", &fD);
        printf("Enter value 5: ");
        scanf("%f", &fE);
        faArray [0] = fA;
        faArray [1] = fB;
        faArray [2] = fC;
        faArray [3] = fD;
        faArray [4] = fE;
      
        printf("The values of original array are : ");
        for (iIndex = 0; iIndex <5; ++iIndex)
           
            printf("%3.2f,  ", faArray[iIndex]);
     
    }
    
    void Modify()
    {
    
    int iIndex;
    float faModifyArray(float [], float);
    float faArray[ArraySize];
    faModifyArray(faArray, ArraySize);
    
    for (iIndex = 0; iIndex < 5; ++iIndex)
        printf("%3.2f,  ", faModifyArray[iIndex]);
    
    }
    int main(void)
    {
       
        void GetArray();
        void Modify();
        GetArray();
        Modify();
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124

    What the....?

    Question: How did you learn C?

    Where did you learn to make an enumaration to define a number?
    You could just say:
    #define ArraySixe 5

    Also:
    Why do you include math.h and stdlib.h ????

    float fA, fB, fC, fD, fE;

    int iIndex;
    float faArray[ArraySize];
    printf("Enter value 1: ");
    scanf("%f", &fA);
    printf("Enter value 2: ");
    scanf("%f", &fB);
    printf("Enter value 3: ");
    scanf("%f", &fC);
    printf("Enter value 4: ");
    scanf("%f", &fD);
    printf("Enter value 5: ");
    scanf("%f", &fE);
    faArray [0] = fA;
    faArray [1] = fB;
    faArray [2] = fC;
    faArray [3] = fD;
    faArray [4] = fE;
    What's that????????????????!!!! Don't you know any other ways to put some values from the user to an array?

    As for the function you wanted:
    Code:
    void faModifyArray( float faArray[], int size )
    {
    	int i;
    
    	for( i=0; i < size; i++ )
    		faArray[i] *= 2;
    
    }
    Suggestion: Forgot whatever you know about functions and arrays and start again with a good review on control structures and then restart with functions
    Loading.....
    ( Trying to be a good C Programmer )

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    21

    i'm new at this

    to answer your question i'm learning c by myself in a self paced course. so i'm having a hard time learning this by myself. my book uses enum {} instead of define for arrays, although it mentions you can use #define. no i don't have a better way to input values into arrays! this is my first programming course yada yada. my problem now is getting the modified array to print, it just shuts down now...thanks for all the help by the way, you've already helped a bunch.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >no i don't have a better way to input values into arrays!
    But you should. Try find one.

    >my problem now is getting the modified array to print, it just shuts down now
    Ah? What do you mean? If you think the problem is with your code post it here.
    Loading.....
    ( Trying to be a good C Programmer )

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Ah! Also use #define instead of that enumaration.
    Loading.....
    ( Trying to be a good C Programmer )

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In fact I think the program is not even legal C89!

    If I remember correctly (I'm a C++ programmer), in C89 the members of enums are not true constants and thus can't be used as array size.
    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

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    21
    my book uses enums i promise. i changed it to define though. the problem with my book is it doesn't say anything about initializing an array with values the user inputs. i just don't get how else to do it, and i know it isn't pretty. the only other way i can think of is to do
    but that didn't work so i thought it was illegal.

    Code:
    float faArray [] = {fA, fB, fC, fD, fE};

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Here's a friendly suggestion for you:
    THROUGH THAT BOOK AWAY!!!!!!

    >float faArray [] = {fA, fB, fC, fD, fE};
    Forget whatever you know about arrays, functions, variables etc, and start from the beginning.
    Loading.....
    ( Trying to be a good C Programmer )

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    21

    next problem

    thanks for the help guys. i figured out how to do that last program, and i even made it look slightly aesthetic. i have another problem though. i'm doing a program that asks the user to imput temperatures for each day of the week for 3 different cities. it prints the high, the low, and the average of each city. i got the high and the low, but how do you find an average of values in an array??

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by CornedBee
    In fact I think the program is not even legal C89!

    If I remember correctly (I'm a C++ programmer), in C89 the members of enums are not true constants and thus can't be used as array size.
    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:
    Code:
    #include <stdio.h>
    
    enum data
    {
      START = 5,
      END
    };
      
    int main(void)
    {
      enum data mydata = START;
      char a1[START]; /* Valid, START in const */
      char a2[mydata]; /* Invalid, mydata in not const */
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    61

    Re: next problem

    Originally posted by faxtoaster
    i got the high and the low, but how do you find an average of values in an array??
    Add the the values of the array ellements and devide the sum by N (number of array elements):

    Code:
    #define N 10
    
    int i, a[N], sum = 0;
    
    for(i = 0; i < N; i++)
      sum += a[i];
    
    printf("Average: %d\n", sum / N);
    Last edited by cc0d3r; 07-17-2003 at 05:44 PM.
    $ENV: FreeBSD, gcc, emacs

  14. #14
    Registered User
    Join Date
    Jul 2003
    Posts
    21

    why is my output huge negative numbers?

    my output using this part of my code gives me huge negative numbers. i'm mixed up as to where my print statement should be in these for loops. am i way off here?

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

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    The for loop going through the array should go before the printf statement. This way fSum var has the meaningfull value:

    Code:
    
    for (p=0; p<Row; p++)
    {   
    
      float fSum;
    
      for (q=0; q<Column; q++)  
         fSum += faArray[p][q];
    
      printf("Average Temperature for City %i: %10.2f\n", p,fSum/7.0);
      
    }
    Btw, I'm not sure if it's a good idea to declare variables inside of the loops, they usually go in the beginning of the function.
    $ENV: FreeBSD, gcc, emacs

Page 1 of 3 123 LastLast
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, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09: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, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21