none
Adding numbers in two arrays into a third array by calling a function in main()

    Question

  • Hi

    I am struggling with the function definition which takes arrays from two pointers?Please see the attached code.

    Many thanks in advance for help!

    /*This program lets the user enter two arrays of ten elements. It then displays the sum of values of two arrays into the third arrays*/
    #include <stdio.h>
    int x,y, array1[10],array2[10],*ptr1,*ptr2, call;
    int sum( int *ptr1, int a, int *ptr2, int b);
    int main()
    {
    	ptr1=&array1[x];
    	ptr2=&array2[y];
    	printf("Enter the 10 elements:\n");
    	for (x=0;x<10;x++)
    	{	
    		scanf("%d",&array1[x]);
    		getchar();
    	}
    
    	printf("Enter the second 10 elements:\n");
    	for (y=0;y<10;y++)
    	{	
    		scanf("%d",&array2[y]);
    		getchar();
    	}
    	printf("the first_array array1 you entered is\n");
    	for (x=0;x<10;x++)
    		{printf("%d\t",array1[x]);}
    	printf("the second_array array2 you entered is\n");
    	for (y=0;y<10;y++)
    		{printf("%d\t",array2[y]);}
    	call=sum(ptr1,10,ptr2,10);
    	printf("The added_array is %d",call);
    	fflush(stdin);
    	getchar();
    	return 0;
    }
    
    int sum( int *ptr1, int a, int *ptr2, int b)
    {
    	
    	int added_array[10];
    	added_array[10]=array1[x]+array2[y];
    	getchar();
    	return added_array[10];
    }
    
    
    
    
    
    
    Thursday, July 08, 2010 7:25 PM

Answers

  • Your pointer variables (ptr1 and ptr2) are not initialized properly (x and y contain garbage when the program starts) and these variables are not even used.  You don't need them.  Get rid of them.

    Instead, pass the arrays (array1 and array2) to the sum function.

    The sum function should use only those variables that are passed to it.  For some reason you are using none of the passed variables in the function, and not using any of the passed variables.

    Where is the loop that adds the two arrays?  Nowhere.

    What is added_array[10]?  It does not exist.  added_array[0] through [9] exist, but you don't do anything to them.

    All in all, it looks very much like you have not read the book.

    • Marked as answer by AustinCann Friday, July 09, 2010 12:26 AM
    Thursday, July 08, 2010 8:45 PM

All replies

  • I am struggling with the function definition which takes arrays from two pointers?

    Struggling - how?

    Please see the attached code.

    This program lets the user enter two arrays of ten elements. It then displays the sum of values of two arrays into the third arrays

    Do you want someone to write it for you?

    int x,y, array1[10],array2[10],*ptr1,*ptr2, call;

    Yuck - put them on separate lines.

    int sum( int *ptr1, int a, int *ptr2, int b);
    int main()
    {
       ptr1=&array1[x];
       ptr2=&array2[y];

    What value do you think x & y are at this point?

    Dave

    Thursday, July 08, 2010 7:54 PM
  • x and y should be zero I guess. Sorry for not making query clear!

    I want to add the corresponding values from array1[x] and array2[y] into a third array which is called in function

    int sum( int *ptr1, int a, int *ptr2, int b);

    Many thanks for help!

     

    Thursday, July 08, 2010 8:00 PM
  • Your pointer variables (ptr1 and ptr2) are not initialized properly (x and y contain garbage when the program starts) and these variables are not even used.  You don't need them.  Get rid of them.

    Instead, pass the arrays (array1 and array2) to the sum function.

    The sum function should use only those variables that are passed to it.  For some reason you are using none of the passed variables in the function, and not using any of the passed variables.

    Where is the loop that adds the two arrays?  Nowhere.

    What is added_array[10]?  It does not exist.  added_array[0] through [9] exist, but you don't do anything to them.

    All in all, it looks very much like you have not read the book.

    • Marked as answer by AustinCann Friday, July 09, 2010 12:26 AM
    Thursday, July 08, 2010 8:45 PM
  • x and y should be zero I guess.

    You guessed wrong. They contain garbage values.

    Sorry for not making query clear!

    I want to add the corresponding values from array1[x] and array2[y] into a third array which is called in function

    int sum( int *ptr1, int a, int *ptr2, int b);

    Many thanks for help!

    Judging from the code you posted, you really need to learn the basics of C++. There is an eBook online that you may find very useful, called Thinking in C++. You should read that or some other introductory C++ book before completing this program.


    Thursday, July 08, 2010 8:47 PM
  • Brian Muth [MVP] wrote:

    x and y should be zero I guess.

    You guessed wrong. They contain garbage values.

    Actually, x and y, being global variables, do in fact happen to be  automatically initialized to zero.


    Igor Tandetnik

    Thursday, July 08, 2010 9:49 PM
  • Thanks for the correction, Igor. I'm duly embarrassed.
    Thursday, July 08, 2010 10:23 PM
  • Thanks for the correction, Igor. I'm duly embarrassed.
    Brian:
     
    I also have trouble remembering the C++ rules for variable initialization. But, like me, you probably don't think about it too much because you
     
    (a) Hardly ever use global variables
     
    (b) To be sure, always explicitly initialize anything you are going to use.
     
    By the way, in your post you appear to be using the NNTP bridge in a way that causes your entire post to appear quoted. In the forum interface, your text is in bold italic so it is possible to distinguish it from the actual quoted text, but I use the NNTP bridge in plain text mode, and it is impossible to tell what is original and what is response.
     
    Actually, one of the reasons I like using the bridge is that it is much easier to do inline responding than it is in the forum interface.
     

    David Wilkinson | Visual C++ MVP
    Friday, July 09, 2010 9:50 AM
  • davewilk [MVP] wrote:

    Brian:
    &nbsp
    I also have trouble remembering the C++ rules for variable  initialization. But, like me, you probably don't think about it too
    much because you &nbsp
    (a) Hardly ever use global variables
    &nbsp
    (b) To be sure, always explicitly initialize anything you are going to  use.
    &nbsp
    By the way, in your post you appear to be using the NNTP bridge in a  way that causes your entire post to appear quoted.

    By the way, you appear to be using NNTP bridge in a way that every empty  line shows up as "&nbsp" for me. All your posts are so afflicted, and no  one else's. Do you perhaps use a custom NNTP client, and forgot a  semicolon in &nbsp; ?


    Igor Tandetnik

    Friday, July 09, 2010 12:23 PM
  • By the way, you appear to be using NNTP bridge in a way that every empty line shows up as " " for me. All your posts are so afflicted, and no one else's. Do you perhaps use a custom NNTP client, and forgot a semicolon in   ?
    Igor:
     
    As always, you are absolutely right!
     
    Indeed, I am using my own HTML converter (a kind of helper application for Thunderbird) with the bridge, and indeed there is one place where I had forgotten the semi-colon in nbsp.
     
    Is it better now?
     
    Thanks so much for pointing this out to me. I had never noticed it because it does not cause a display error on the forums themselves, or when reconverted to text by my plain text converter (which uses the clipboard).
     
    I also am duly embarrassed.
     

    David Wilkinson | Visual C++ MVP
    Friday, July 09, 2010 1:36 PM
  • By the way, you appear to be using NNTP bridge in a way that every empty line shows up as " " for me. All your posts are so afflicted, and no one else's. Do you perhaps use a custom NNTP client, and forgot a semicolon in ?
    Igor:
     
    As always, you are absolutely right!
    And before you point out that in quoting your post I was not converting the "&" in "&nbsp" to an entity, I think I have fixed that too now (going forward, not retroactively).
     

    David Wilkinson | Visual C++ MVP
    Friday, July 09, 2010 2:42 PM
  • davewilk [MVP] wrote:

    By the way, you appear to be using NNTP bridge in a way that every  empty line shows up as "&nbsp" for me. All your posts are so
    afflicted, and no one else's. Do you perhaps use a custom NNTP  client, and forgot a semicolon in ?

    Igor:

    As always, you are absolutely right!

    Indeed, I am using my own HTML converter (a kind of helper application  for Thunderbird) with the bridge, and indeed there is one
    place where I had forgotten the semi-colon in nbsp.

    Is it better now?

    Yes, looks OK now.


    Igor Tandetnik

    Friday, July 09, 2010 4:37 PM