# Maximum PC

 It is currently Sat Apr 25, 2015 4:13 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Dynamic arraysPosted: Thu Mar 10, 2005 8:05 pm
 Clawhammer

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1053 - Computer Science II
//  Program #3
//  This program dynamically creates two arrays
//  It then adds them together and then prints to screen/file.
///////////////////////////////////////////////////////////////////////////////

#include <string>                              // String header file
#include <iostream>                              // Input Output stream header file
#include <fstream>
#include <string>

using namespace std;

void input();
void calculations(int * pointer_1, int * pointer_2, int * pointer_3, int biggest);

int main ()                                    // Main function
{                                          // Opens main function

int *pointer_1, *pointer_2, *pointer_3;
string entry_1, entry_2;
int first, second, temp, biggest;
ofstream outfile;
outfile.open("output.txt",ios::app);

// reads in the numbers to a string
cout << "Enter the first number:  ";
cin>>entry_1;
first = entry_1.size();
cout << "Enter the second number:  ";
cin>>entry_2;
second = entry_2.size();

pointer_1 = new int[first];
pointer_2 = new int[second];

for(int n = 0; n < first; n++)
{
pointer_1[first-1-n] = entry_1[n]-'0';
}

for( n = 0; n < second; n++)
{
pointer_2[second-1-n] = entry_2[n]-'0';
}

if(first>second)
biggest=first;
else
biggest=second;

int carry=0;
pointer_3 = new int[biggest];

for(n = 0; n < biggest; n++)
{
pointer_3[n] = carry + pointer_2[n] + pointer_1[n];

if(pointer_3[n] > 9)
carry=1;

else
carry=0;
}

for(n = biggest-1; n>= 0; n--)
cout<<pointer_3[n];

delete [] pointer_1;
delete [] pointer_2;
delete [] pointer_3;

outfile.close();
return 0;

}

void calculations(int * pointer_1, int * pointer_2, int * pointer_3, int biggest)
{

}

Last edited by alex911 on Thu Mar 10, 2005 9:07 pm, edited 1 time in total.

Top

 Post subject: Posted: Thu Mar 10, 2005 9:07 pm
 Clawhammer

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1053 - Computer Science II
//  Program #3
//  This program dynamically creates two arrays
//  It then adds them together and then prints to screen/file.
///////////////////////////////////////////////////////////////////////////////

#include <string>                              // String header file
#include <iostream>                              // Input Output stream header file
#include <fstream>
#include <string>

using namespace std;

void input(int &biggest,int first, int second,string entry_1,string entry_2, int * pointer_1, int * pointer_2);
void calculations(int carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest);
void print(int carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest);

int main ()                                    // Main function
{                                          // Opens main function

int *pointer_1, *pointer_2, *pointer_3;
string entry_1, entry_2;
int carry=0;
int first, second, biggest;
ofstream outfile;
outfile.open("output.txt",ios::app);

input(biggest, first,  second, entry_1, entry_2,   pointer_1,   pointer_2);
calculations( carry, entry_1, entry_2,  pointer_1,   pointer_2,   pointer_3,  biggest);
print( carry, entry_1, entry_2, pointer_1,   pointer_2,   pointer_3,  biggest);

delete [] pointer_1;
delete [] pointer_2;
delete [] pointer_3;

outfile.close();
return 0;

}

void calculations(int carry, string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest)
{

pointer_3 = new int[biggest];

for(int n = 0; n < biggest; n++)
{
pointer_3[n] = carry + pointer_2[n] + pointer_1[n];

if(pointer_3[n] > 9)
carry=1;

else
carry=0;
}

}

void input(int &biggest, int first, int second,string entry_1,string entry_2, int * pointer_1, int * pointer_2)
{

// reads in the numbers to a string
cout << "Enter the first number:  ";
cin>>entry_1;
first = entry_1.size();
cout << "Enter the second number:  ";
cin>>entry_2;
second = entry_2.size();

pointer_1 = new int[first];
pointer_2 = new int[second];

for(int n = 0; n < first; n++)
{
pointer_1[first-1-n] = entry_1[n]-'0';
}

for( n = 0; n < second; n++)
{
pointer_2[second-1-n] = entry_2[n]-'0';
}

if(first>second)
biggest=first;
else
biggest=second;

}

void print(int carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest)
{

for(int n = biggest-1; n>= 0; n--)
cout<<pointer_3[n];

}

Top

 Post subject: Posted: Thu Mar 10, 2005 9:23 pm
 Clawhammer

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1053 - Computer Science II
//  Program #3
//  This program dynamically creates two arrays
//  It then adds them together and then prints to screen/file.
///////////////////////////////////////////////////////////////////////////////

#include <string>                              // String header file
#include <iostream>                              // Input Output stream header file
#include <fstream>
#include <string>

using namespace std;

void input(int &biggest,int &first, int &second,string entry_1,string entry_2, int * pointer_1, int * pointer_2);
void calculations(int first, int second,int &carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest);
void print(int carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest);

int main ()                                    // Main function
{                                          // Opens main function

int *pointer_1, *pointer_2, *pointer_3;
string entry_1, entry_2;
int carry=0;
int first, second, biggest;
ofstream outfile;
outfile.open("output.txt",ios::app);

input(biggest, first,  second, entry_1, entry_2,   pointer_1,   pointer_2);
calculations(first, second, carry, entry_1, entry_2,  pointer_1,   pointer_2,   pointer_3,  biggest);
print( carry, entry_1, entry_2, pointer_1,   pointer_2,   pointer_3,  biggest);

delete [] pointer_1;
delete [] pointer_2;
delete [] pointer_3;

outfile.close();
return 0;

}

void calculations(int first, int second,int &carry, string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest)
{

if(first>second)
biggest=first;
else
biggest=second;

pointer_3 = new int[biggest];

for(int n = 0; n < biggest; n++)
{
pointer_3[n] = carry + pointer_2[n] + pointer_1[n];

if(pointer_3[n] > 9)
carry=1;

else
carry=0;
}

}

void input(int &biggest, int &first, int &second,string entry_1,string entry_2, int * pointer_1, int * pointer_2)
{

// reads in the numbers to a string
cout << "Enter the first number:  ";
cin>>entry_1;
first = entry_1.size();
cout << "Enter the second number:  ";
cin>>entry_2;
second = entry_2.size();

pointer_1 = new int[first];
pointer_2 = new int[second];

for(int n = 0; n < first; n++)
{
pointer_1[first-1-n] = entry_1[n]-'0';
}

for( n = 0; n < second; n++)
{
pointer_2[second-1-n] = entry_2[n]-'0';
}

}

void print(int carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest)
{

for(int n = biggest-1; n>= 0; n--)
cout<<pointer_3[n];

}

Top

 Post subject: Posted: Thu Mar 10, 2005 9:27 pm
 Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
1.) Why all the posts? Just edit the first one.

2.) I assume you can't just use the vector container provided by the standard library?

3.) What is your question?

4.) Wouldn't it be better to create a dynamic array class (of course, in real-world programming you would just use std::vectors), and create two objects of those?

Top

 Post subject: Posted: Thu Mar 10, 2005 9:58 pm
 Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
After talking on AIM we got it. Just posting the final result here:

Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1053 - Computer Science II
//  Program #3
//  This program dynamically creates two arrays
//  It then adds them together and then prints to screen/file.
///////////////////////////////////////////////////////////////////////////////

#include <string>                              // String header file
#include <iostream>                              // Input Output stream header file
#include <fstream>
#include <string>

using namespace std;

void input(int &biggest,int &first, int &second, string &entry_1, string &entry_2, int *& pointer_1, int *& pointer_2);
void calculations(int first, int second,int &carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int *&pointer_3, int &biggest);
void print(int carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest);

int main ()                                    // Main function
{                                          // Opens main function

int *pointer_1 = 0, *pointer_2 = 0, *pointer_3 = 0;
string entry_1, entry_2;
int carry=0;
int first, second, biggest;
ofstream outfile;
outfile.open("output.txt",ios::app);

input(biggest, first,  second, entry_1, entry_2,   pointer_1,   pointer_2);

calculations(first, second, carry, entry_1, entry_2,  pointer_1,   pointer_2,   pointer_3,  biggest);

print( carry, entry_1, entry_2, pointer_1,   pointer_2,   pointer_3,  biggest);

delete [] pointer_1;
delete [] pointer_2;
delete [] pointer_3;

std::cin.ignore(1000, '\n');
std::cin.get();
outfile.close();
return 0;

}

void calculations(int first, int second,int &carry, string entry_1,string entry_2,int *pointer_1, int * pointer_2, int *&pointer_3, int &biggest)
{
if(first>second)
biggest=first;
else
biggest=second;

pointer_3 = new int[biggest];

for(int n = 0; n < biggest; n++)
{
pointer_3[n] = carry + pointer_2[n] + pointer_1[n];

if(pointer_3[n] > 9)
carry=1;

else
carry=0;
}
}

void input(int &biggest, int &first, int &second, string &entry_1, string &entry_2, int *&pointer_1, int *&pointer_2)
{
// reads in the numbers to a string
cout << "Enter the first number:  ";
cin>>entry_1;
first = entry_1.size();
cout << "Enter the second number:  ";
cin>>entry_2;
second = entry_2.size();

pointer_1 = new int[first];
pointer_2 = new int[second];

for(int n = 0; n < first; n++)
{
pointer_1[first-1-n] = entry_1[n]-'0';
}

for( n = 0; n < second; n++)
{
pointer_2[second-1-n] = entry_2[n]-'0';
}
}

void print(int carry,string entry_1,string entry_2,int * pointer_1, int * pointer_2, int * pointer_3, int biggest)
{
for(int n = biggest-1; n>= 0; n--)
cout<<pointer_3[n];
}

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 3 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group