Like Tree1Likes

Pointer arithmetic

This is a discussion on Pointer arithmetic within the C++ Programming forums, part of the General Programming Boards category; I am writing a program for an array with 10 elements. I am trying to get the sum of all ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    12

    Pointer arithmetic

    I am writing a program for an array with 10 elements. I am trying to get the sum of all the elements in the array. I have thoroughly confused myself. Please look at my code and make suggestions as to what I am doing wrong. I know it is in the second 'for' loop, but I know that I am making it harder than what it should be.




    Code:
    #include <iostream>
    using namespace std;
    void explanation();
    int sums;
    
    
    int main()
    {
    	explanation();
    	int nums[10] = {1,2,3,4,5,6,7,8,9,10};
    	int i;
    	int *ptr;
    	ptr=&nums[0];
        printf("The numbers in the array are:    \n");
    
    	for (i=0; i < 10; i++)
    	{
    		
    		printf("%d  ", nums[i]);
    
    	}
    
    	
    	for (i=0; i < 10; i++)
    	{ 
    		
    	sums = *ptr + nums[i];
    		
    	}
    
    	printf( "%d","The sum of the elements are:   ", sums );
    
    	return 0;
    }
    
    void explanation()
    {
    	printf("Written by Kelle McCan\n");
    	printf("This program calculates the sum of a ten\n");
    	printf("element array and then displays the sum\n");
    	printf("\n");
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    int sums=0;
    sums += nums[i];

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    I feel I'm getting closer! However, now it is saying the sum is 0. That's better than the 3294506849 I was getting!

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    printf( "The sum of the elements are: %d", sums );

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    It too is still giving the value of 0. I tried playing with that, giving the value of 55 (as what it should be) and it still came up with 0. Do you think it might be how I declared them?

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Did you change
    int sums;
    to
    int sums=0;

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    yes I did. Still getting '0' for the sums.

    I haven't changed much.
    Code:
    #include <iostream>
    using namespace std;
    //void explanation();
    int sums;
    
    
    int main()
    {
    	explanation();
    	int nums[10] = {1,2,3,4,5,6,7,8,9,10};
    	int i;
    	int *ptr;
    	ptr=0;
    	ptr=&nums[10];
        printf("The numbers in the array are:    \n");
    
    	for (i=0; i < 10; i++)
    	{
    		
    		printf("%d  ", nums[i]);
    
    	}
    
    	
    	for (i=0; i <= 10; i++)
    	{ 
    		int sums=0;
    	    sums += nums[i];
    		
    	}
    
    	printf("\n");
    	printf("The sum of the elements are:   ");
    	printf("\n");
    	printf("%d", sums);
    	printf("\n");
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    You have 2 sums (delete both).
    Delete: int sums;
    Also delete: int sums=0;
    Change: int i;
    to: int i,sums=0;

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    I am sorry. I am now getting a negative number. It's very long. I did as you asked. Do I need a prototype? As you can see by my posts prior to this, I have problems understanding pointers.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    You are currently not even using the pointers and can temporarily delete:
    Code:
    int *ptr;
    ptr=0;
    ptr=&nums[10];
    We are first trying to find your problem.

    Try adding more printf's everywhere to see the math.

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Ok...I'm commenting on your code on post #7 ...
    Do accordingly... and it'll be ok.
    Code:
    #include <iostream>
    using namespace std;
    //void explanation();
    
    int sums;
    
     
    
     
    
    int main()
    {
    
        explanation();
    
        int nums[10] = {1,2,3,4,5,6,7,8,9,10};
    
        int i;
    
        int *ptr;                //Why do you need these ?
    
        ptr=0;                     //^
    
        ptr=&nums[10];//^
    
        printf("The numbers in the array are:    \n"); //Try using C++ style I/O ..
    
     
    
        for (i=0; i < 10; i++)
    
        {
    
             
    
            printf("%d  ", nums[i]);
    
     
    
        }
    
     
    
         
    
        for (i=0; i <= 10; i++) //< instead of <= ...as indices are from 0 to n-1
    
        { 
            int sums=0;//Put this statement above the loop.
    
            sums += nums[i];
    
             
        }
    
     
    
        printf("\n");
    
        printf("The sum of the elements are:   ");
        printf("\n");
    
        printf("%d", sums);
    
        printf("\n");
    
     
        return 0;
    
    }
    Also.. just to tempt you towards using the modern C++ features..watch the following code..(Note that you'll need a C++11 compatible compiler)
    Code:
    #include<array>
    #include<iostream>
    using namespace std;
    int main()
    {
        array<int,10> nums={1,2,3,4,5,6,7,8,9,10};
        int sum(0);
        for(auto x:nums)
        {
            cout<<x<<'\t';
            sum+=x;
        }
        std::cout<<"\nSum= "<<sum;
        cin.get();
        return 0;
    }
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    I guess I didn't clearly state what I was doing. In my assignment I am to write a program that has an array of 10 elements and use pointer arithmetic to sum up all of the elements in the array. I don't have a problem with adding the arrays. It's using pointers that I am having a hard time understanding. I know that I can use sums = *p + (*p+1) + (*p+2) +....(*p+9), but I know there is an easier way, so in the mean time I keep confusing myself.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    motocross1, I don't know it you read my other reply to manasij7479, but I am trying to use pointer arithmetic to sum an array that has 10 elements. I appreciate the help you have given me, honestly.

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Same thing..
    Change the loop to:
    Code:
    for(int i=0;i<n;i++)
        sum+=*(array_name + i);
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    Yay! Thank you so much!!! I knew I was making it harder than what it should be. Thank you so much!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer arithmetic
    By danieldcc in forum C Programming
    Replies: 8
    Last Post: 09-30-2011, 01:17 PM
  2. Pointer arithmetic
    By _arjun in forum C Programming
    Replies: 1
    Last Post: 09-20-2011, 12:06 PM
  3. Pointer Arithmetic
    By taurus in forum C Programming
    Replies: 5
    Last Post: 11-14-2008, 03:28 AM
  4. Pointer arithmetic
    By freebu in forum C Programming
    Replies: 4
    Last Post: 04-27-2003, 10:27 AM
  5. pointer arithmetic
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-04-2001, 07:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21