The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Adding Elements Within An Array
Discuss Adding Elements Within An Array in the C Programming forum on Dev Shed. Adding Elements Within An Array C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 28th, 2004, 05:58 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Location: Missouri
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Adding Elements Within An Array
Here is what my program is supposed to do.
Using to premade functions we made previously StringToInt and InputLongInt write two functions: AddLongInts and DisplayLongInt. Write a main program that uses these functions in order to repeatedly input two large integers, add them, and display their sum.
I'm having a couple of problems figuring out what to do.
1. How do I add these individual elements in an array.
Last edited by Rex Overbey : January 29th, 2004 at 01:29 AM.
|

January 29th, 2004, 09:03 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
|
all that says is to write two functions:
1. AddLongInts just adds two integers together and returns the result.
2. DisplayLongInt uses cout to display the result returned by the function above.
You should have a main() function in which you create a loop that continusly prompts for the two integers, call AddLongInts to add them together, then call DisplayLongInt() to display the result.
|

January 29th, 2004, 10:47 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hi, Rex Overbey
can you be little clear.
|

January 30th, 2004, 02:52 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Location: Missouri
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
already figured it out here it is
/* Author: Rex Overbey
Lab number: CS265 Open Lab 1
Due Date: Friday : 01/30/03
Purpose: Input two large integers, add them, and display their sum.
***********************************************************************************/
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <iomanip>
#include <cassert>
using namespace std;
const int CAPACITY = 300; //type declarations
typedef int longIntArray[CAPACITY];
void InputLongInt (longIntArray longInt, int & length); //prototypes
int StringToInt (string s);
int AddLongInts (int first, int second);
void DisplayLongInt(longIntArray longInt, int len);
int main()
{
char choice; //choice to continue program
cout << "This program functions in order to repeatedly "
<< "input two large integers, add them, and display their sum.\n\n";
do{
int length=0; //variable declartions for lengths and carry value
int length2=0;
int alength=0;
int car=0;
longIntArray LongInteger,
LongInteger2,
answer;
InputLongInt(LongInteger, length); //first function call
InputLongInt(LongInteger2,length2); //second function call
for (int i=(length-1);i>=0;i--)
{ answer[i+1]=car+AddLongInts(LongInteger[i], LongInteger2[i]);
if(answer[i+1]>=1000)
{
answer[i+1]=answer[i+1]-1000;
car=1;
}
else
{
car=0;
answer[i]=car;
alength=i+1;
}
}
DisplayLongInt(answer, alength);
cout<<"\nDo you want to run the program again:";
cin>>choice;
}
while(choice == 'Y' || choice =='y');
cout <<"\n\nEnd of program";
}
/**********************************************************************************/
// *** Implementation of InputLongInt:
void InputLongInt (longIntArray longInt, int & length)
{
string inString;
cout << "\nEnter a long integer with commas, e.g. 1,218,921: ";
cin >> inString;
int pos;
while (inString != "")
{
length++;
pos = inString.rfind(",", inString.size() - 1);
if (pos == string::npos)
{
longInt[length-1] = StringToInt(inString);
inString = "";
}
else
{
longInt[length-1] = StringToInt(inString.substr(pos+1, 3));
inString.erase(pos, 4);
}
}
}
/* Library of string operations.
*/
/* StringToInt: Receives a string s.
Precondition: s contains only digits, and represents an integer
which is small enough to be stored as an int.
Returns: the integer equivalent of the string s.
***********************************************************************************/
int StringToInt (string s)
{
int result = 0;
int placeValue = 1;
int digit;
for (int i = s.size()-1; i >= 0; i--)
{
assert (isdigit(s[i]));
digit = s[i] - '0';
result += digit * placeValue;
placeValue *= 10;
}
return result;
}
/* AddLongInts: Receives first and second integer.
Precondition: first and second in their raw forms
Post condition: Answer stores total.
***********************************************************************************/
int AddLongInts(int first, int second)
{
int answer;
answer = first + second;
return answer;
}
/*DisplayLongInt: Recieves anwer from AddLongInts()
Precondition: Receives Answer and Len
Postcondition:Outputs to the screen.
***********************************************************************************/
void DisplayLongInt(longIntArray answer, int len)
{
cout << "\nThe answer is: ";
for (int i=(len-1);i>=0;i--)
{
cout << " " << answer[i];
}
cout<<"\n\n";
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|