Thread: Memorizing the changes in an array after it has been used by a function

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    17

    Memorizing the changes in an array after it has been used by a function

    When I use the function multiplyPolynomials (the last function in the code) the function works as intended but as soon as the debugger is done with executing the function the numbers in the array result[] disappear.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    int readPolynomial(int poly[]);
    void printPolynomial(int numCoefs, int poly[]);
    void multiplyPolynomials(int poly1[], int poly2[], int result[]);
    
    void main()
    {
    	int polynome1[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
    	int polynome2[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
    	int result[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
    
    	printf("Numerator of polynomial 1:\n");
    	int numerator1 = readPolynomial(polynome1[0]);
    	printPolynomial(numerator1, polynome1[0]);
    	printf("denominator of polynomial 1:\n");
    	int denominator1 = readPolynomial(polynome1[1]);
    	printPolynomial(denominator1, polynome1[1]);
    	printf("Numerator of polynomial 2:\n");
    	int numerator2 = readPolynomial(polynome2[0]);
    	printPolynomial(numerator2, polynome2[0]);
    	printf("denominator of polynomial 2:\n");
    	int denominator2 = readPolynomial(polynome2[1]);
    	printPolynomial(denominator2, polynome2[1]);
    	multiplyPolynomials(polynome1[0], polynome2[1], result[0]);
    	printPolynomial(numerator1, result[0]);
    }
    
    int readPolynomial(int poly[])
    {
    	int number_of_coefficients = 0;
    	printf("Number of coefficients? ");
    	scanf("%i", &number_of_coefficients);
    	printf("Please enter %i coefficients: ", number_of_coefficients);
    	for(int i = 0; i<number_of_coefficients; ++i)
    	{
    		scanf("%i", &poly[i]);
    	}
    	return number_of_coefficients;
    }
    
    
    void printPolynomial(int numCoefs, int poly[])
    {
    	printf("%i*x^0 + %i*x^1 + %i*x^2 + %i*x^3 + %i*x^4", poly[0], poly[1], poly[2], poly[3], poly[4]);
    }
    
    void multiplyPolynomials(int poly1[], int poly2[], int result[])
    {
    	int sum = 0;
    	for(int i = 0; i <5; ++i)
    	{
    		for(int j = 0; j<5; ++j)
    		{
    			sum = poly1[i]*poly2[j];
    			result[i+j] = sum + result[i+j];
    		}
    	}
    
    }
    If I let the debugger stop just before finishing the function multiplyPolynomials and I check the array result[] then I get the correct results (and the intended results), as soon as the debugger has finished with the function that array is empty.
    If I let the debugger execute the entire program then the last call of the printPolynomial function doesn't work.

    As far as I understood an array automatically works with pointers so everything that is being done to the array by a function should be stored during the remainder of the execution of the program (as long as you don't change anything in the array).

    I tried to solve the problem by returning the result-array in the function multiplyPolynomials.
    Though that seemed to work (the numbers are still there in the array result[] after the function multiplyPolynomial has been executed) the printfunction still doesn't work.
    Furthermore I think that this is not particularly an elegant method.

    Code:
    #include<stdio.h>
    int readPolynomial(int poly[]);
    void printPolynomial(int numCoefs, int poly[]);
    int multiplyPolynomials(int poly1[], int poly2[], int result[]);
    
    void main()
    {
    	int polynome1[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
    	int polynome2[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
    	int result[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
    
    	printf("Numerator of polynomial 1:\n");
    	int numerator1 = readPolynomial(polynome1[0]);
    	printPolynomial(numerator1, polynome1[0]);
    	printf("denominator of polynomial 1:\n");
    	int denominator1 = readPolynomial(polynome1[1]);
    	printPolynomial(denominator1, polynome1[1]);
    	printf("Numerator of polynomial 2:\n");
    	int numerator2 = readPolynomial(polynome2[0]);
    	printPolynomial(numerator2, polynome2[0]);
    	printf("denominator of polynomial 2:\n");
    	int denominator2 = readPolynomial(polynome2[1]);
    	printPolynomial(denominator2, polynome2[1]);
    	multiplyPolynomials(polynome1[0], polynome2[1], result[0]);
    	printPolynomial(numerator1, result[0]);
    }
    
    int readPolynomial(int poly[])
    {
    	int number_of_coefficients = 0;
    	printf("Number of coefficients? ");
    	scanf("%i", &number_of_coefficients);
    	printf("Please enter %i coefficients: ", number_of_coefficients);
    	for(int i = 0; i<number_of_coefficients; ++i)
    	{
    		scanf("%i", &poly[i]);
    	}
    	return number_of_coefficients;
    }
    
    
    void printPolynomial(int numCoefs, int poly[])
    {
    	printf("%i*x^0 + %i*x^1 + %i*x^2 + %i*x^3 + %i*x^4", poly[0], poly[1], poly[2], poly[3], poly[4]);
    }
    
    int multiplyPolynomials(int poly1[], int poly2[], int result[])
    {
    	int sum = 0;
    	for(int i = 0; i <5; ++i)
    	{
    		for(int j = 0; j<5; ++j)
    		{
    			sum = poly1[i]*poly2[j];
    			result[i+j] = sum + result[i+j];
    		}
    	}
    	for(int i = 0; i<9; ++i)
    	{
    		return result[i];
    	}
    
    }
    What am I doing wrong?

    Note that this program is far from finished. For example: right now I am not doing anything with the variable numCoefs (printPolynomial) but I am going to use that in a conditional statement in the function printPolynomial so that only the x'es with a non-zero coefficient are being printed. So please ignore these kind of things.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Your first program looks OK (apart from the void main).
    The only thing I added was to call printPolynomial at the end of multiplyPolynomials as well.

    Code:
    $ gcc -std=c99 -Wall foo.c
    foo.c:7: warning: return type of ‘main’ is not ‘int’
    $ ./a.out 
    Numerator of polynomial 1:
    Number of coefficients? 3
    Please enter 3 coefficients: 1 2 3
    1*x^0 + 2*x^1 + 3*x^2 + 0*x^3 + 0*x^4
    denominator of polynomial 1:
    Number of coefficients? 3
    Please enter 3 coefficients: 4 5 6
    4*x^0 + 5*x^1 + 6*x^2 + 0*x^3 + 0*x^4
    Numerator of polynomial 2:
    Number of coefficients? 3
    Please enter 3 coefficients: 7 8 9
    7*x^0 + 8*x^1 + 9*x^2 + 0*x^3 + 0*x^4
    denominator of polynomial 2:
    Number of coefficients? 3
    Please enter 3 coefficients: 2 4 6
    2*x^0 + 4*x^1 + 6*x^2 + 0*x^3 + 0*x^4
    Result in function
    2*x^0 + 8*x^1 + 20*x^2 + 24*x^3 + 18*x^4
    ---
    2*x^0 + 8*x^1 + 20*x^2 + 24*x^3 + 18*x^4
    Looks OK in the debugger as well
    Code:
    $ gcc -std=c99 -Wall -g foo.c
    foo.c:7: warning: return type of ‘main’ is not ‘int’
    $ gdb ./a.out 
    GNU gdb (GDB) 7.1-ubuntu
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/sc/Documents/coding/a.out...done.
    (gdb) break 26
    Breakpoint 1 at 0x400730: file foo.c, line 26.
    (gdb) run
    Starting program: /home/sc/Documents/coding/a.out 
    Numerator of polynomial 1:
    Number of coefficients? 3
    Please enter 3 coefficients: 1 2 3
    1*x^0 + 2*x^1 + 3*x^2 + 0*x^3 + 0*x^4
    denominator of polynomial 1:
    Number of coefficients? 3
    Please enter 3 coefficients: 4 5 6
    4*x^0 + 5*x^1 + 6*x^2 + 0*x^3 + 0*x^4
    Numerator of polynomial 2:
    Number of coefficients? 3
    Please enter 3 coefficients: 7 8 9
    7*x^0 + 8*x^1 + 9*x^2 + 0*x^3 + 0*x^4
    denominator of polynomial 2:
    Number of coefficients? 3
    Please enter 3 coefficients: 2 4 6
    2*x^0 + 4*x^1 + 6*x^2 + 0*x^3 + 0*x^4
    Result in function
    2*x^0 + 8*x^1 + 20*x^2 + 24*x^3 + 18*x^4
    ---
    
    Breakpoint 1, main () at foo.c:26
    26	    printPolynomial(numerator1, result[0]);
    (gdb) print result[0]
    $1 = {2, 8, 20, 24, 18, 0, 0, 0, 0}
    (gdb) next
    2*x^0 + 8*x^1 + 20*x^2 + 24*x^3 + 18*x^4
    27	}
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    Weird (in the sense that I thought I carefully checked the array both in a seperate debugscreen and in the code itself (Microsoft Visual C++ 2010 express), it seems to work after all.
    I have tried it out once more and indeed: the array result[] does store the right numbers after the functioncall has been finished.
    Then how does it come that the function printPolynomial doesn't work on this array?
    I use the right arguments, don't I? (the first argument needs to be given but won't be used in the function so it shouldn't matter which argument I give as long as it is an integer)

    It is essential for my program that the function printPolynomial is called from main because I want to calculate fractions. So if I want to add or subtract fractions then I need to call the function multiplyPolynomial three times and I need to sum the result of the first two calls: (numerator1 * denominator 2 + numerator2*denominator1) / denominator1 * denominator2.
    So after the seconde multiplication the function printPolynomial should be executed with the argument array[]
    The only reason I already used the function printPolynomial after the first multiplication was to check out if it worked.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    Of course the summation automatically happens as soon as I call the multiplPolynomialfunction for the second time:
    Code:
    multiplyPolynomials(polynome1[1], polynome2[0], result[0]);
    I just checked the array result[] and it works fine. My only problem: the printfunction still doesn't work from main for that array, it does work for the other two arrays.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why are you declare result as a 2D array instead of like this

    Code:
    int result[9] = {0,0,0,0,0,0,0,0,0};
    You are only using a single dimension of it.

    Tim S.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    I am also going to use the second dimension of that array, I just haven't used it yet.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    Never mind. Everything works as it should. The rest won't be a problem.

    Your first program looks OK (apart from the void main).
    What's wrong with that void?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Harry Reve View Post
    Never mind. Everything works as it should. The rest won't be a problem.


    What's wrong with that void?
    The standard requires main to return an int.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Question about memorizing thousands of API func calls
    By blankstare77 in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2005, 03:10 AM