-
reprint originals
I am trying to get the program print the original values after it prints the sqares.
how can i do that? Where are they stores since values[] changes after sqaring them?
Code:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
void square(int[], int);
main(){
int values[]={5,3,7,12,9,1,10,8,4,3};
int i;
for (i=0; i<SIZE; i++){
printf ("%i ",values[i]);}
printf("\n");
square(values, SIZE);
for (i=0; i<SIZE; i++){
printf("%i\n",values[i]);}
system("Pause");
}
void square(int data[],int size){
int i;
for (i=0; i<SIZE; i++){
data[i]=data[i]*data[i];
}
}
-
Once you have overwritten the content of values, the original data is "lost". You will need to have a different array to store either the original value or the modified value.
Edit: of course, the constants used to initialize the array are there in your executable - but the format and location is completely dependent on the implementation of the compiler, and it may well be that your constants are part of the generated machine code (e.g. mov , or that a block of data known to the compiler is copied into the location of the "values" array. The conclusion of this is that you can't "just get the original values back".
--
Mats
-
what i would like to do is store the new values somethere else. How would i do that, because from what i see it automatically saves them in the same array as the original ones.
-
Create another array of the same size and pass that to the function as well.
-
this is what i have but it does not look like newValues is taken back to main. how can i fix that?
Code:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
void square(int[], int);
main(){
int values[]={5,3,7,12,9,1,10,8,4,3};
int i;
int newValues[SIZE];
for (i=0; i<SIZE; i++){
printf ("%i ",values[i]);}
printf("\n");
square(values, SIZE);
for (i=0; i<SIZE; i++){
printf("%i\n",newValues[i]);}
for (i=0; i<SIZE; i++){
printf("%i\n",values[i]);}
system("Pause");
}
void square(int data[],int size){
int i;
int newValues[SIZE];
for (i=0; i<SIZE; i++){
newValues[i]=data[i]*data[i];
}
}
-
You did not pass newValues to square(). You need to provide square() with another parameter for newValues to be passed as an argument.
-
this is what i have so far what am i doing wrong?
Code:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
void square(int[], int[],int);
main(){
int values[]={5,3,7,12,9,1,10,8,4,3};
int i;
int newValues[SIZE];
for (i=0; i<SIZE; i++){
printf ("%i ",values[i]);}
printf("\n");
square(values, newValues, SIZE);
for (i=0; i<SIZE; i++){
printf("%i\n",newValues[i]);}
for (i=0; i<SIZE; i++){
printf("%i\n",values[i]);}
system("Pause");
}
void square(int data[],int data[],int size){
int i;
for (i=0; i<SIZE; i++){
newValues[i]=data[i]*data[i];
}
}
-
Change:
Code:
void square(int data[],int data[],int size){
to:
Code:
void square(int data[],int newValues[],int size){
Other than that, remember that main() returns an int, so declare it as returning int and then return 0 at the end.
-
-
Code:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
void square(int data[], int newValues[], int size);
int main(){
int values[SIZE]={5,3,7,12,9,1,10,8,4,3};
int i;
int newValues[SIZE];
for (i=0; i<SIZE; i++){
printf ("%i ",values[i]);
}
printf("\n");
square(values, newValues, SIZE);
for (i=0; i<SIZE; i++){
printf("%i\n",newValues[i]);
}
for (i=0; i<SIZE; i++){
printf("%i\n",values[i]);
}
getchar();
return 0;
}
void square(int data[], int newValues[], int size){
int i;
for (i=0; i<SIZE; i++){
newValues[i]=data[i]*data[i];
}
}
I do suggest that you avoid placing brackets like you did. It's extremely easy to miss them. Plus you botched the indentation level on the following line, increasing the illusion that would make you think that the line is part of the loop and that you missed an ending }.
See my changes in red.
-
And whilst we're picking nit: When declaring a prototype function, use names for the arguments:
Code:
void square(int data[], int newData[], int size);
And of course, Elysia didn't fix the argument name being duplicated in the function definition.
--
Mats
-
Made a couple of more changes.
Defined the first array with SIZE too. It makes it easier to detect errors since the arrays should be equally big.
Replaced system with getchar, fixed the arguments, the prototype and an explicit return at the end of main.
-
a side question here.
can we do this:
Code:
int array1[] = { 1, 2, 3 };
int array2[sizeof(array1)];
-
It compiles under C++, so I don't see why not.
-
Quote:
Originally Posted by
manav
a side question here.
can we do this:
Code:
int array1[] = { 1, 2, 3 };
int array2[sizeof(array1)];
It appears to compile, although you may want [sizeof(array1)/sizeof(array1[0])] to give you the same number as array1.
--
Mats