JameB 66 Junior Poster

I think we need more code to analyze this. If the program is less than about 250 lines, post it with line numbers:

[code=cplusplus] // paste code here

[/code]

That way there's no guesswork. The problem could be in the string-reversal, the negative handling, the absolute value handling, etc. If it's a big program, post a fragment, but a COMPLETE fragment please, so we can run it.

Is there a reason you don't just convert the strings to integers and subtract normally, or is this an assignment where you are supposed to do it digit by digit?

I am suppose to be able to add numbers upto 100000 digits with negative signs.


Here is what I have so far..

// Make a new substraction function that goes from right to left instead of left to right.
// - start at (len - 1) eg. If (len == 5) then start at 4. If (len == 10) then start at 9
// - make your way left towards zero
// You can't just substract one from the number left to it, you have to make sure that you find a number that is bigger than
// '48'... Then add ten to next number and then continue.....

#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;

int absolute(int input) // Takes in a number and returns the absolute value of that number.
{
	if (input >= 0)
	{
		input = input;
	}
	else if (input < 0)
	{
		input …
JameB 66 Junior Poster

I am trying to write a program that given two integers in two strings, substracts the second integer from the first integer and outputs it.

1. I read in two strings.
2. I reverse them (I am also adding them, I found it easier to add if they are reversed)
3. I check if any of them are negative...
4. If both are negative or no negatives, I add them up.
5. If any ONE of them is negative, I substract the second string from the first string. <<<< Problem >>>>
6. Store the answer in an integer array.
7. Output the integer array BACKWARDS.


Here is what I have so far for substraction, but it doesn't work for some numbers. It gives weird answers like (-1) + 10000 = 90001.

for (c = 0; c < len; c++)
		{
			// If the first number is bigger, then its simple.
			if((	one[c] > two[c]) || (two[c] == one[c]))
			{
				sum[c] = absolute((one[c] - 48) - (two[c] - 48));
			}
			else if (two[c] > one[c])
			{
				// If not then find it actual value, add ten to it and then substract it.. :)
				sum[c] = absolute(((one[c] + 10) - 48) - (two[c] - 48));
				
				// Then take one away from the next number in 'one'				
				one[c+1] -= 1;
			}
		}

Can someone help me here? I don't get what I am doing wrong...

JameB 66 Junior Poster
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int count = 0, a;
	char first[99999], second[99999], sum[100000], carry;
	
	gets(first);
	count = strlen(first);
	gets(second);
	carry = '0';

	for(a = 0; a < count; a++)
    {
		sum[a] = (first[a] + second[a] + carry) - '0';
		carry = '0';

		if(sum[a] > ('9' - '0'))
		{
			sum[a] -= '9';
			carry = '1';
		}
		cout << sum[a];
    }
    return 0;
}

here is what I came up so far I can't get the

sum[number] -= '9';

to work, but if you do, please post the solution here. Thank you and your welcome!! :P

JameB 66 Junior Poster

Here is the problem statement for which I need to make this program. I hope this clarifies stuff

A plus B (2)

Given two positive or negative integers A and B, find their exact sum.

This time there are no limitations on A and B (of course, they will fit in memory).

A and B are less than 99999 digits.
You have 3 seconds of time to answer each test case.
There are ten test cases. Marked out of 100 making each test case worth 10 points. Good luck!
JameB 66 Junior Poster

How do you find the length of an int array??

I have int array with some values and I need to count how many values are there?

I tried a lot of things like strlen(); sizeof(); etc..

int main()
{

int iarray[10000];

// Now I have a lot of code here.

cout << "the length of the array is " << endl;

return 0;
}

so how do I find the length of the array and no I am not reading in the values straight to the int array, I read it in char array then convert it to string then reverse it and then back to char array and then I convert that char array into an int array. I then need to run a for loop. Thats why I need to find the length of the array.

Thanks in advance.

JameB 66 Junior Poster

uhmmm... Why don't you just loop through all the bits in the value shifting it over, then checking it with a logic AND(mask 1). Add "1" to it if it's true, else "0".
Simple?

What do you mean?

JameB 66 Junior Poster
void IsPrimeNumber (void)
{
	int Num, count;
	
	cout << "Please enter number to check if it's prime--> ";
	cin >> Num;
	cout << endl << endl;
       
       for (int x = 1; x <= Num; x++)
       {
               if (Num % x == 0)
               {
                      count++;
               }
        }

        if (count == 2)
        {
                 cout << "Your number is prime." << endl;
         }
         else
          {
                   cout << "Not a prime" << endl;
          }

      
}

Hope that helps....

JameB 66 Junior Poster

I cannot use itoa();

I already did that and it won't compile on my teacher's computer. Apparently she is using some website's judging process and it has a g++ compiler. Since itoa() is not a standard function, g++ doesn't support it.

JameB 66 Junior Poster

You can make a variable and put it as the last line in your loop and output the variable that you created AFTER the loop has finished.

JameB 66 Junior Poster

Hello, I am trying to create a program where I can work with an array of char that contain either 0 or 1 representing a decimal number that the user enters.

Basically I need to convert a decimal number into binary and store it into a char array. I wrote this program but it is outputting something weird.

char binary(int number)
{
	int temp = 0, c = 0;
	char buffer;

	if (number % 2 == 0)
	{
		temp = number / 2;
	}
	else
	{
		number = number - 1;
		temp = number / 2;
	}
	
	if (temp % 2 == 0)
	{	
		buffer = '0';
	}
	else
	{
		buffer = '1';
	}

	return buffer;
}

int main()
{
	int num;

	cin >> num;

	while(num != 0)
	{
		cout << binary(num);
		
		if (num % 2 == 0)
		{
			num = num / 2;
		}
		else
		{
			num = num - 1;
			num = num / 2;
		}
	}

	return 0;
}

Can someone help me out??
JameB 66 Junior Poster

I don't know the purpose of all those for loops. If all you want to do is erase the '~' characters then just do that

for(int i = 0; i < inp.length(); i++)
{
   if( inp[i] == '~')
      inp.erase(i,1);
}

That is whats not working. It only deletes half of the "~" symbols..

This is what I already have..

for (int i = 0; i < strlen(inp.c_str()); i++)
	{
		if (inp[i] == 126)
		{
			inp.erase(i, 1);
		}
	}

So if I input "~~~~" then it will only delete two of them not all four of them.

I think its because when you erase the character the next one takes its place and the for loop will not delete the one that takes the deleted one's place...

JameB 66 Junior Poster

Hello, I have a problem where I need to delete any occurrence of "~" from a string. I created a function but in the end, it only deletes half of the "~" and leaves the other half....

string goodstring(string inp) 
{
	for (int i = 0; i < strlen(inp.c_str()); i++)
	{
		if (inp[i] >= 0x41 && inp[i] <= 0x5A)
		{
			inp[i] += 0x20;
		}
	}

	for (int i = 0; i < strlen(inp.c_str()); i++)
	{
		if (inp[i] >= 32 && inp[i] <= 47)
		{
			inp[i] = 126;
		}
	}

	for (int i = 0; i < strlen(inp.c_str()); i++)
	{
		if (inp[i] >= 58 && inp[i] <= 64)
		{
			inp[i] = 126;
		}
	}

	for (int i = 0; i < strlen(inp.c_str()); i++)
	{
		if (inp[i] >= 91 && inp[i] <= 96)
		{
			inp[i] = 126;
		}
	}

	for (int i = 0; i < strlen(inp.c_str()); i++)
	{
		if (inp[i] >= 123 && inp[i] <= 126)
		{
			inp[i] = 126;
		}
	}

	for (int i = 0; i < strlen(inp.c_str()); i++)
	{
		if (inp[i] >= '0' && inp[i] <= '9')
		{
			inp[i] = 126;
		}
	}

	for (int i = 0; i < strlen(inp.c_str()); i++)
	{
		if (inp[i] == 126)
		{
			inp.erase(inp.begin() + i, inp.begin()+i+1);
		}
	}

	return inp;
}

So what I basically do is read a string, and go like

input = goodstring(input);

Can someone help me sort this problem? Also I just realized that I don't actually need that many for loops.. Sorry.