Thread: reprint originals

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    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];
    	}
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Last edited by matsp; 04-02-2008 at 06:44 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Create another array of the same size and pass that to the function as well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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 ("&#37;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];
    	}
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not pass newValues to square(). You need to provide square() with another parameter for newValues to be passed as an argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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 ("&#37;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];
    	}
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thanks it works now.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 ("&#37;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.
    Last edited by Elysia; 04-02-2008 at 07:06 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Banned
    Join Date
    Nov 2007
    Posts
    678
    a side question here.

    can we do this:
    Code:
    int array1[] = { 1, 2, 3 };
    int array2[sizeof(array1)];

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It compiles under C++, so I don't see why not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed