C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 28th, 2004, 05:58 PM
Rex Overbey Rex Overbey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Missouri
Posts: 2 Rex Overbey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old January 29th, 2004, 09:03 PM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
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.

Reply With Quote
  #3  
Old January 29th, 2004, 10:47 PM
vaninagam vaninagam is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 vaninagam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi, Rex Overbey

can you be little clear.

Reply With Quote
  #4  
Old January 30th, 2004, 02:52 PM
Rex Overbey Rex Overbey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Missouri
Posts: 2 Rex Overbey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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";
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Adding Elements Within An Array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap