Need help with displaying a result

This is a discussion on Need help with displaying a result within the C++ Programming forums, part of the General Programming Boards category; Hello again everyone, it's been awhile since I was here. I have another problem for anyone who is up for ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23

    Need help with displaying a result

    Hello again everyone, it's been awhile since I was here. I have another problem for anyone who is up for the challenge. I have a code here that I will post in a second, and what it is is a code that someone inputs 2 strings of numbers. The code then turns the strings of numbers into 2 integer arrays. It then adds the 2 arrays piece by piece and inputs that data into a new array. I am having problems with carrying a 1 to a new decimal place beyond the highest place inputed. Example being inputs are 900 and 100, it will display 000 and give me an error. Does anyone know how I could put this last decimal place in? I have tried for about 5 hours now and am finally turning to you all. Thanks again!
    Code:
    /*a program that allows someone to input a Very Long Integer(VLI), then adds and shows the sum and then compares
    	the two values */
    
    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <string>
    using namespace std;
    void vliInput (int [], string, int&);
    void vliOutput (const int [], const int);
    void addVli (const int [], const int [], int[], const int, const int);
    void compVli (const int [], const int[]);
    const int MAXDIGITS = 100;
    
    int main()
    {
    	int vli1 [MAXDIGITS] = {0}, vli2[MAXDIGITS] = {0}, sum[MAXDIGITS] = {0}, size1 = 0, size2 = 0;
    	string num1, num2;
    	vliInput(vli1, num1, size1);//calls function to input first number
    	vliOutput(vli1, size1);//displays the number in integer form in an array
    	vliInput(vli2, num2, size2);//calls function to input second number
    	vliOutput(vli2, size2);//displays second number in integer form in an array
    	addVli(vli1, vli2, sum, size1, size2);//preforms additions
    	compVli(vli1, vli2);//compares the two arrays
    	return 0;
    }
    
    void vliInput (int vli[], string num, int& size)// input values for the string
    {
    	cout << "Enter in a number between 1 and 100 digits: ";
    	getline(cin,num);//person inputs the number
    	size = num.length();//counts the amount of numbers inputed
    	for (int i = 0; i < size; i++)//turns the numbers into an array counting right to left
    		vli[MAXDIGITS - i - 1] = num[i] - 48;
    }
    
    void vliOutput (const int vli[], const int size)//output the number
    {
    	for (int i = 0; i < size; i++)
    		cout << vli[MAXDIGITS - i -1];
    	cout << endl;
    }
    
    void addVli(const int vli1[], const int vli2[], int sum[], const int size1, const int size2)//adds the arrays
    {
    	int size3 = 0;
    	int cr = 0;//integer used for a 1 carried
    	for (int i = 0; i < size1 || i < size2; i++)
    	{
    		sum[MAXDIGITS - 1 - i] = (vli1[MAXDIGITS - 1 - i] + vli2[MAXDIGITS - 1 - i])%10;//gets the sum
    		sum[MAXDIGITS - i + 1] += cr;//adds the carried one
    		cr = (vli1[MAXDIGITS - 1 - i] + vli2[MAXDIGITS - 1 - i])/10;//finds if there is a carried one
    		size3++;//counter for the size of the size for the total integer
    	}
    		if(cr == 1)//attempts to make the new space to add the 1, but has failed to do so
    			{
    				sum [MAXDIGITS - size3] += cr;
    				size3++;
    			}
    	for(int i = 0; i < size3; i++)//displays the number
    		cout << sum[MAXDIGITS - 1 - i];
    	cout << endl;
    }
    
    void compVli(const int vli1[], const int vli2[])//compares the two numbers
    {
    	for(int i = 0; i < MAXDIGITS; i++)
    	{
    		if(vli1[i] > vli2[i])
    		{
    			cout << "Vli1 is larger than Vli2." << endl;
    			break;
    		}
    		else if (vli1[i] < vli2[i])
    		{
    			cout << "Vli2 is larger than Vli1." << endl;
    			break;
    		}
    		else if (vli1[i] == vli2[i] && i == MAXDIGITS - 1)
    			cout << "Vli1 and Vli2 are equal." << endl;
    	}
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> for (int i = 0; i < size1 || i < size2; i++)

    I think you should actually start at the other end of the array. Think of it like this:

    Code:
      101
    + 109
      --- 
      110
    You start on the right hand side of the sum. It's more logical and less complicated than the other method. While you're doing the same thing, it's nicer to read with a:

    Code:
    for (int i = MAXZIZE-1; i>0; i--)
    In my opinion.

    Anyways. Create a temp variable which is the sum of two current elements. Add them together. Add the remainder (%), of it to the sum element, and the most significant number to the element one place higher than the current bit.

    Code:
    Array1: [4][6][2]
    Array2: [1][5][3]
    Starting from the right:

    Code:
    sum: 2+3 = 5,  5%10=5 (place to current sum element), Array2[1]+=sum/10
    sum: 5+6 = 11, 5%10=1 (place in current sum element), Array2[0]+=sum/10
    etc.
    Just write some code around that, and come back here if you're still stuck.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    I am reading over your code and I don't think I quite understand it. I have all the addition down. If you put in any number that doesn't need an additional space on the end, it adds them up pretty well. It's just any number that adds another decimal place that cuases an error. Sorry about the delay on the response, was AFK getting some supper.
    WARNING: Do not click this link if you are distracted too easily http://blueballfixed.ytmnd.com/

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    One other error I just noticed. If I add numbers such as 25 and 250, i will get 500 instead of 275. I tried to correct this error earlier as well but failed. I set the initial values added to start at size3, which i made size3 the larger of the two sizes, and it gave me a jumble of errors. I am not sure how to fix this error either. I worked on that problem for many hours as well, but as I said, I still have no idea on where to go with it.

    Edit: I would have put this in the statement above but i accidently did quick reply, sorry.
    Last edited by Daesom; 11-30-2006 at 04:50 PM.
    WARNING: Do not click this link if you are distracted too easily http://blueballfixed.ytmnd.com/

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    As Towmers suggested, start doing the sum at the end of the array and moving backwards to the front one element at a time. You then need to determine if the sum of those elements carry some value over to the next element. How do you do it on paper?

    27
    +
    36

    The sum of 7 and 6 is 13. You write 3 down and carry 1 over to the next sum adding it and repeating the process. 1 + 2 + 3 = 6. You write 6 down. The result is 63.

    How to code if a number carries over or not? There's probably more than one way. One of them is to divide the result of each sum by 10 and take the integer portion.

    13 / 10 = 1.3 ---- > static_cast<int>(1.3) = 1
    6 / 10 = 0.6 -----> static_cast<int>(0.6) = 0

    You don't need to worry about the result. You just add it to the next elements. If its zero it will simply add 0, not having any effect .

    How to determine what to write on the new array for that element sum? It's similar to the above, but now you want the fractional part.

    13 / 10 - static_cast<int>(13 / 10) * 10 = 3
    6 / 10 - static_cast<int>(6 / 10) * 10 = 6

    That is the number you add to the new array
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Well I thank you both and anyone else who wants to share their ideas with me. I will try to figure this out using the ideas you both presented and anyone else presents. I am only in an intro to C++ class and my professor wants us to do this. Honestly, I feel this is sorta hard for a person in an intro to C++ class, but it is his class and I suppose he wants us to think about it, but I have tried for many hours. I will continue to try to figure this out, thanks again!
    WARNING: Do not click this link if you are distracted too easily http://blueballfixed.ytmnd.com/

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    Well, to make the multiple digit thing easier, you could add zeros at the beginning. e.g.
    12 +
    1341
    ->
    0012 +
    1341

    Also, adding another zero to the beginning would allow for carrying of the first digit, e.g.
    81 +
    32
    ->
    081 +
    032
    so that you can get 113 instead of 13 or an error

    Just an idea.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23

    To anyone interested

    Good news! I took a break, went back to my code, did some cout stuff in certain places to see what was going on and I figured it out! I can sometimes amaze myself! I would LOVE if someone would copy this code and throw gernades and nukes at it to see if there are any faults in it. Please, please do so, and if you find any let me know!

    Code:
    /*a program that allows someone to input a Very Long Integer(VLI), then adds and shows the sum and then compares
    	the two values */
    
    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <string>
    using namespace std;
    void vliInput (int [], string, int&);
    void vliOutput (const int [], const int);
    void addVli (const int [], const int [], int[], const int, const int);
    void compVli (const int [], const int[]);
    const int MAXDIGITS = 100;
    
    int main()
    {
    	int vli1 [MAXDIGITS] = {0}, vli2[MAXDIGITS] = {0}, sum[MAXDIGITS] = {0}, size1 = 0, size2 = 0;
    	string num1, num2;
    	vliInput(vli1, num1, size1);//calls function to input first number
    	vliOutput(vli1, size1);//displays the number in integer form in an array
    	vliInput(vli2, num2, size2);//calls function to input second number
    	vliOutput(vli2, size2);//displays second number in integer form in an array
    	addVli(vli1, vli2, sum, size1, size2);//preforms additions
    	compVli(vli1, vli2);//compares the two arrays
    	return 0;
    }
    
    void vliInput (int vli[], string num, int& size)// input values for the string
    {
    	do
    	{
    	cout << "Enter in a number between 1 and 100 digits: ";
    	getline(cin,num);//person inputs the number
    	size = num.length();//counts the amount of numbers inputed
    	}while(size > 100);
    	for (int i = 0; i < size; i++)//turns the numbers into an array counting right to left
    		vli[MAXDIGITS - i - 1] = num[size - 1 -i] - 48;
    }
    
    void vliOutput (const int vli[], const int size)//output the number
    {
    	for (int i = 0; i < size; i++)
    		cout << vli[MAXDIGITS - size + i];
    	cout << endl;
    }
    
    void addVli(const int vli1[], const int vli2[], int sum[], const int size1, const int size2)//adds the arrays
    {
    	int size3 = 0;
    	int cr = 0;//integer used for a 1 carried
    	for (int i = 0; i < size1 || i < size2; i++)
    	{
    		cout << vli1[MAXDIGITS - 1 - i] << "\t" << vli2[MAXDIGITS - 1 - i] << endl;
    		sum[MAXDIGITS - 1 - i] = (vli1[MAXDIGITS - 1 - i] + vli2[MAXDIGITS - 1 - i])%10;//gets the sum
    		sum[MAXDIGITS - i - 1] += cr;//adds the carried one
    		cr = (vli1[MAXDIGITS - 1 - i] + vli2[MAXDIGITS - 1 - i])/10;//finds if there is a carried one
    		size3++;//counter for the size of the size for the total integer
    	}
    		if(cr == 1)//attempts to make the new space to add the 1, but has failed to do so
    			{
    				sum [MAXDIGITS - size3 - 1] += cr;
    				size3++;
    			}
    	for(int i = 0; i < size3; i++)//displays the number
    		cout << sum[MAXDIGITS - size3 + i];
    	cout << endl;
    }
    
    void compVli(const int vli1[], const int vli2[])//compares the two numbers
    {
    	for(int i = 0; i < MAXDIGITS; i++)
    	{
    		if(vli1[i] > vli2[i])
    		{
    			cout << "Vli1 is larger than Vli2." << endl;
    			break;
    		}
    		else if (vli1[i] < vli2[i])
    		{
    			cout << "Vli2 is larger than Vli1." << endl;
    			break;
    		}
    		else if (vli1[i] == vli2[i] && i == MAXDIGITS - 1)
    			cout << "Vli1 and Vli2 are equal." << endl;
    	}
    }
    Edit: A small edit, I do believe the error I was creating was putting the numbers at the end of the array from right to left in decreasing 10ths places, instead of increasing 10ths places. Therefore it would count 450 as 054 and say 50 as 05 so when 450 +50 was inputed, it would produce 054+005=059 or as i had it output, 900. I fixed that small problem and it works like a charm now. Go ahead and put in 100 decimal digits and see if you can get the program to create an error. (if you put in 100 you can't let the 100th 10th's place total more than 10)

    Edit2:Adding a checker to make sure the number is 100 digits or less.
    Last edited by Daesom; 11-30-2006 at 07:35 PM.
    WARNING: Do not click this link if you are distracted too easily http://blueballfixed.ytmnd.com/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 12:44 AM
  3. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  4. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 03:04 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21