its not. i think i heard someone say it only works under c99, something alot of compilers have problems with.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.I'm not sure if it's a good idea to declare variables inside of the loops
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); }
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!
No, I think I confused it withOriginally 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:
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
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 1Originally 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.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
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
Of course. They're inclosed in a scope block. Anything defined in a new scope is only visible to said scope: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..
Quzah.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 */
Hope is the first step on the road to disappointment.
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]); } } }
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 )
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.
>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 )
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]); } } }
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
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; }
Try this page:
http://www.cprogramming.com/tutorial/lesson8.html
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