Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How can I make pointers to make this easier?

$
0
0
I am getting confused between the memory address and the value of how pointers work. How would I use this program to ask the user to enter a set of large integer numbers and add the arrays together but with pointers?


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>
#include <sstream>

int const SIZE = 5;
using namespace std;

void inputValue1(long int[]);
void inputValue2(long int[]);
void Sum(long int array1[], long int array2[]);

main()
{
	long int A[SIZE];
	long int B[SIZE];
	long int C[SIZE];

	inputValue1(A);
	inputValue2(B)/>;
	Sum(A, B)/>;

	getch();
}

void inputValue1(long int A[])
{
	cout << "First array" << endl;
	for(int i=0; i<SIZE; i++)
	{
		cout << "Enter digit " << i + 1 << " of " << SIZE << ":";
		cin >> A[i];
		if (A[i] > 9 || A[i] < 0)
		{
			cout << "Enter a digit between 0 and 9" << endl;
			i --;
		}
		cout << "\n";
	}
	return;
}

void inputValue2(long int B[])
{
	cout << "Second array" << endl;
	for(int i=0; i<SIZE; i++)
	{
		cout << "Enter digit " << i + 1 << " of " << SIZE << ":";
		cin >> B[i];
		if (B[i] > 9 || B[i] < 0)
		{
			cout << "Enter a digit between 0 and 9" << endl;
			i --;
		}
		cout << "\n";
	}
	return;
}

void Sum(long int array1[], long int array2[])
{
	string firstArrayStr;
	string secondArrayStr;
	long int firstNum;
	long int secondNum;
	long int result;
	stringstream firstStream;
	stringstream secondStream;

	// First array
	for (int counter = 0; counter < SIZE; counter++)
	{
		firstStream << array1[counter];
		// Add this array element to string stream
		firstArrayStr = firstStream.str();
	}

	// Second array
	for (int counter = 0; counter < SIZE; counter++)
	{
		secondStream << array2[counter];
		secondArrayStr = secondStream.str();
	}

	// Convert strings to integers
	firstNum = atoi(firstArrayStr.c_str());
	secondNum = atoi(secondArrayStr.c_str());

	result = firstNum + secondNum;

	cout << "result = " << result << endl;
}




Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>