• Advertisement
Sign in to follow this  

Array / Pointer problem

This topic is 4369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok I am having some trouble getting this code to work the problem lies in the "while" statement here is the code any help and/or explanation would be appreciated!
//Header: AddNums.h

//Function Prototypes:
  //Add 2 numbers
  long AddNumbers(long Num1, long Num2);

  //Add an array of numbers:
  long AddNumbers(long *NumArray, long NumOfNums);



//program: OverloadFun.cpp
  
#include <iostream>
#include "AddNums.h"
using namespace std;

//Fuction Code
long AddNumbers(long Num1, long Num2){
	return Num1+Num2;
}
long AddNumbers(long *NumArray, long NumOfNums){
	long Result; 
		long i;
	

	Result = 0;
	while(NumOfNums--)
		Result+= NumArray;

		return Result;
}

int main() {
	 long Result = AddNumbers(10,20); //30 
	cout <<"Result of two numbers added = "<<Result<<endl;
	long Array[5] = {10,20,30,40,50};
	 long Result2= AddNumbers(Array, 5); //150
	cout <<"Result of combined array total = "<<Result2 << endl;
	system("SLEEP");
} 

Share this post


Link to post
Share on other sites
Advertisement
i is initialized, therefore NumArray points ... somewhere that's unlikely to actually be in the array.

Furthermore, if you want to sum all the values, you need to make your index change somehow, otherwise, you'll just be adding the same value over and over.

You can try either something like:
for(i=0; i<NumOfNums; ++i)
Result+= NumArray;


or something like
i = 0;
while(NumOfNums--)
{
Result += NumArray
++i;
}


Or something equivalent.

Share this post


Link to post
Share on other sites
while(NumofNums--) goes on forever. you want it to stop at 0 but when it is 0 it sees "hey I can substract 1 from NumofNums to get -1 and so on

Share this post


Link to post
Share on other sites
Quote:
Original post by Vegettex
while(NumofNums--) goes on forever. you want it to stop at 0 but when it is 0 it sees "hey I can substract 1 from NumofNums to get -1 and so on


Not true.

It evaulates the variable "NumofNums", if its zero it quits the loop. Otherwise, it decrements it and program flow continues for another iteration.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by Vegettex
while(NumofNums--) goes on forever. you want it to stop at 0 but when it is 0 it sees "hey I can substract 1 from NumofNums to get -1 and so on


Not true.

It evaulates the variable "NumofNums", if its zero it quits the loop. Otherwise, it decrements it and program flow continues for another iteration.


Ouch, yeah, I am still sleeping....shouldn't go out anymore :P

long AddNumbers(long *NumArray, long NumOfNums)
{
long Result = 0, i = 0;
while(NumOfNums--)
Result+= NumArray[i++];
return Result;
}

should just work fine :)

bah alcohol :) ahwel, I am awake now :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement