Quantcast

Maximum PC

It is currently Thu Sep 18, 2014 7:19 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Dynamic arrays
PostPosted: Thu Mar 10, 2005 8:05 pm 
Clawhammer
Clawhammer
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 9:07 pm 
Clawhammer
Clawhammer
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 9:23 pm 
Clawhammer
Clawhammer
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 9:27 pm 
Team Member Top 100
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? :lol:

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
  Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 9:58 pm 
Team Member Top 100
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
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group