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.
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.
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 )
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; }
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 ????
What's that????????????????!!!! Don't you know any other ways to put some values from the user to an array?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;
As for the function you wanted:
Suggestion: Forgot whatever you know about functions and arrays and start again with a good review on control structures and then restart with functionsCode:void faModifyArray( float faArray[], int size ) { int i; for( i=0; i < size; i++ ) faArray[i] *= 2; }
Loading.....
( Trying to be a good C Programmer )
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.
>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 )
Ah! Also use #define instead of that enumaration.
Loading.....
( Trying to be a good C Programmer )
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
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};
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 )
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??
The method used in the code in the post above is valid, as ArraySize is a true constant.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.
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]
Add the the values of the array ellements and devide the sum by N (number of array elements):Originally posted by faxtoaster
i got the high and the low, but how do you find an average of values in an array??
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
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]; }
The for loop going through the array should go before the printf statement. This way fSum var has the meaningfull value:
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.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); }
$ENV: FreeBSD, gcc, emacs