rfrapp 17 Junior Poster in Training

If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.

take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.

OR in pseudocode, maybe somehting like this:

x, y, remainder 
remainder = y
while x less than or equal to remainder
  remainder = remainder - x

Is there an easier approach that I'm not seeing?

rfrapp 17 Junior Poster in Training

Hi rfrapp,

I'm guessing your 'home' button doesn't actually link directly to index.php but just to the root directory. When a directory address is requested, the default action is for it to load index.html

You can change this by adding the following line to a .htaccess file DirectoryIndex index.php index.html The default action will now be to try and load index.php and if it can't find that to load index.html

Thank you very much! So where exactly would that file be located? Sorry, I'm new to PHP.

rfrapp 17 Junior Poster in Training

Do checked your home menu URL ? What u have provide? index.php (or) index.html ?

The name of the file is: "index.php," and all the links link to "index.php"

rfrapp 17 Junior Poster in Training

Well, you you are going backwards and just broke your solution.

What is remainder?
What is the result of hex[remainder]?
What happens to that result without an = sign somewhere?
How does changing [remainder] to [answer % 16] solve your problem?

You need to start thinking through the problems, not using a shot-gun technique and hoping you hit upon the solution by trial and error.

Although it is a different task, this thread has a bunch of concepts spelled out you can use while trying to understand this weird programming lifestyle.

I think I've got it now, except for one part. Here's what I've done:

//converts answer in decimal to hex
void backToHex( double answer )
{
	char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};	
	int remainder[11] = {0};

	if (remainder[11] != 0)
	{
		cout << "----------Addition Overflow----------" << endl;
	}
	remainder[0] = answer / 16; 
	//remainder[1] = not sure what to put here, as I cannot use modulus
	remainder[2] = answer / 256; //16 to the 2
	remainder[3] = answer / 4096; //16 to the 3
	remainder[4] = answer / 65536; //16 to the 4
	remainder[5] = answer / 1048576; //16 to the 5
	remainder[6] = answer / 16777216; //16 to the 6
	remainder[7] = answer / 268435456; //16 to the 7
	remainder[8] = answer / 4294967296; //16 to the 8
	remainder[9] = answer / 68719476736; //16 to the 9

	for (int i …
rfrapp 17 Junior Poster in Training

Well, you you are going backwards and just broke your solution.

What is remainder?
What is the result of hex[remainder]?
What happens to that result without an = sign somewhere?
How does changing [remainder] to [answer % 16] solve your problem?

You need to start thinking through the problems, not using a shot-gun technique and hoping you hit upon the solution by trial and error.

Although it is a different task, this thread has a bunch of concepts spelled out you can use while trying to understand this weird programming lifestyle.

I realize that those things aren't what I need to do. I have an algorithm for what I need to do, but I'm not quite sure how to do it.
-Use an array of 10, with each element of the array representing the number of 16^whatever power's are in the answer
-Index hex array with the values of the above array

That sounds better in my head, I hope it makes sense.

rfrapp 17 Junior Poster in Training

I've fixed the problem that I had earlier by adding another function to do the addition. Adding two digits together words perfectly, unless the answer is more than two digits. I'm not quite sure how to do this for more than one digit. Any suggestions? I can't post code right now, because I'm on a different computer, but I can provide what I've done later if necessary. Thanks!

rfrapp 17 Junior Poster in Training

Except what's that loop for?

And what are you doing with hex[remainder];?


No, just inexperienced. We were all there at one time...

I figured out how to get the second digit, however, my problem from an earlier post still applies. If I enter "AB," then that in decimal is 171. If the second input is "1," then 1 + 171 would be 172. However, if I divide 172 by 16, then it spits out a "D." Rather than an "A," which it should be. It seems as though it has converted the second input to 49. Is that an error in my convertToDec function? How would I fix it?

rfrapp 17 Junior Poster in Training

Except what's that loop for?

And what are you doing with hex[remainder];?


No, just inexperienced. We were all there at one time...

I don't need the loop. I realize that now, and as for the "hex[remainder]," I changed it to "hex[answer % 16];" which gives me the last digit. I'm not quite sure how to proceed...

rfrapp 17 Junior Poster in Training

Ouch!
Use a character array for the HEX characters. Use remainder as an index into that array to 'convert' the remainder into a hex digit. Now your 30-line conversion is 2 lines.
That it should make it easier to see what you need to do next.

This is what you meant right?

char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	

	for (int k = 0; remainder < 10; k++)
	{
                remainder = a % 16;
		hex[remainder];
	
	}

I know you probably think I'm dumb... But I'm trying to understand. Arrays are very new to me

WaltP commented: YES!! Plus what deceptikon said... +17
rfrapp 17 Junior Poster in Training

Here's the function that I've made to do this, however, it's not perfect. It only works correctly for single digit numbers. Double digits and up are problematic.

//converts answer in decimal to hex
void backToHex( double answer )
{
	int remainder;
	int a = answer;
	int b = a / 16;
	char hex[11];
	for (int k = 1; k < 10; k++)
	{
		remainder = a % 16;

		if (remainder == 10)
			hex[k] = 'A';
		else if (remainder == 11)
			hex[k] = 'B';
		else if (remainder == 12)
			hex[k] = 'C';
		else if (remainder == 13)
			hex[k] = 'D';
		else if (remainder == 14)
			hex[k] = 'E';
		else if (remainder == 15)
			hex[k] = 'F';
		hex[0] = b;
		if (b == 10)
			hex[0] = 'A';
		else if (b == 11)
			hex[0] = 'B';
		else if (b == 12)
			hex[0] = 'C';
		else if (b == 13)
			hex[0] = 'D';
		else if (b == 14)
			hex[0] = 'E';
		else if (b == 15)
			hex[0] = 'F';
	}
	if (hex[11] > 0)
		{
			cout << "----------Addition Overflow----------" << endl;
		}
	else 
		cout << hex[0] << hex[1] << endl;
}

If I entered AB for the first input, my output would be: D, but it should be A. Why is that? Is it because "answer" was a double and I changed it to an int? And I doubt that this will handle multiple digits. Any help would be greatly appreciated.

rfrapp 17 Junior Poster in Training

Are you allowed to use standard C functions? sprintf() can do all that for you, just a matter of giving it the correct forma spcification. E.g. "%x" converts to hex and "%f" to float

Unfortunately, no. I need to write my own function to do it

rfrapp 17 Junior Poster in Training

Let me start off by saying that this is homework, and I have most of it completed. However, I am stuck on how to convert decimal to hexadecimal. The assignment is to add two hexadecimal numbers and output the answer. If the length of the answer is greater than 10 digits, then output "Addition Overflow." I've completed the conversion of the two strings to decimal and added them together, now I think that I need to convert the answer back to a string. I know it's long, but if you would take the time to read my code, then it would be greatly appreciated. Here's my code:

// hex addition.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


void convertToDec(string, string);
void backToHex(double);

int _tmain(int argc, _TCHAR* argv[])
{

	//assign values to last six hex characters
	//convert both inputs to dec
	//add two dec numbers
	//convert back go hex for answer

	string input;
	string input2;
	int digit = 0;
	bool decision;
	char choice;

	cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
	
	do
	{
		decision = true;
		cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
		cin >> input;
		cout << "Now enter another hexadecimal number to add to the first: ";
		cin >> input2;
		if (input.length() > 10)
		{
			cout << "Invalid input" << endl;
			system("pause");
			return 0;
		}
		convertToDec(input, input2);
		cout << "Would you like to continue? (y/n)";
		cin …
rfrapp 17 Junior Poster in Training

If you have the value "ABCDEF" in input, what is the value of input.length()?

If i = input.length(), which character is input[i] ?
Remember, zero based values... :icon_wink:

In that case it's be 7 right? I'm sorry, arrays confuse me xD

rfrapp 17 Junior Poster in Training

I didn't try to understand what you are doing in your code. But, I see one mistake.
Variables like digit10 which is of int type cannot hold such a big value.
If int is of 4 bytes, it can go only till 2147483647 but digit10 is assigned with 68719476736.

It seems the assertion failure is coming from my for statement in line 81. The purpose of this for loop is to print the "input" string backwards, and assign those values the the array of "convert" array. How do you think I can modify this so that I won't have an assertion error? The error it said is "Expression: string subscript out of range."

rfrapp 17 Junior Poster in Training

I'm making a website, and my index is a php file, whereas the other 6 pages are html. Whenever I click on the "Home" button, it says that it can't find index.html, but that's obvious because it's called index.php. Any way to fix this? If I'm being too vague, then please let me know what information I can provide you with to help you help me. Thank you!

rfrapp 17 Junior Poster in Training

I didn't try to understand what you are doing in your code. But, I see one mistake.
Variables like digit10 which is of int type cannot hold such a big value.
If int is of 4 bytes, it can go only till 2147483647 but digit10 is assigned with 68719476736.

Ah, okay. I'll fix that and see how it works out. Thanks!

rfrapp 17 Junior Poster in Training

I'm writing a program to add two hex numbers of up to ten digits, and I'm recieving
an "assertion failure," and I can't figure out why. Any help would be greatly appreciated. Thank you for you time!

// hex addition.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>


using namespace std;

void convertToDec(string, string);

int _tmain(int argc, _TCHAR* argv[])
{

	//assign values to last six hex characters
	//convert both inputs to dec
	//add two dec numbers
	//convert back go hex for answer

	string input;
	string input2;
	string hex[16] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
	int digit = 0;
	bool decision;
	char choice;

	cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
	
	do
	{
		decision = true;
		cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
		cin >> input;
		cout << "Now enter another hexadecimal number to add to the first: ";
		cin >> input2;
		if (input.length() > 10)
		{
			cout << "Invalid input" << endl;
			system("pause");
			return 0;
		}
		convertToDec(input, input2);
		cout << "Would you like to continue? (y/n)";
		cin >> choice;
		if (choice == 'n' || choice == 'N')
		{
			decision = false;
			system("pause");
			return 0;
		}
		if (choice == 'y' || choice == 'Y')
		{
			decision = true;
			system("pause");
			system("cls");
		}

	} while (decision = true);
	system("pause");
	return 0;
}

void convertToDec(string input, string input2)
{
	int convert[10] = {0}; …
rfrapp 17 Junior Poster in Training

I'm writing a program that adds two hexadecimal numbers of up to ten digits.
I'm trying to use the POW function, but for some reason it's underlined. Why is that?

// hex addition.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>


using namespace std;

void convertToDec(string, string);

int _tmain(int argc, _TCHAR* argv[])
{

	//assign values to last six hex characters
	//convert both inputs to dec
	//add two dec numbers
	//convert back go hex for answer

	string input;
	string input2;
	string hex[16] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
	int digit = 0;
	bool decision;
	char choice;

	cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
	
	do
	{
		decision = true;
		cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
		cin >> input;
		cout << "Now enter another hexadecimal number to add to the first: ";
		cin >> input2;
		if (input.length() > 10)
		{
			cout << "Invalid input" << endl;
			system("pause");
			return 0;
		}
		convertToDec(input, input2);
		cout << "Would you like to continue? (y/n)";
		cin >> choice;
		if (choice == 'n' || choice == 'N')
		{
			decision = false;
			system("pause");
			return 0;
		}
		if (choice == 'y' || choice == 'Y')
		{
			decision = true;
			system("pause");
			system("cls");
		}

	} while (decision = true);
	system("pause");
	return 0;
}

void convertToDec(string input, string input2)
{
	int convert[10] = {0};
	int result;
	int digit1 = pow(16, 0); …
rfrapp 17 Junior Poster in Training

1) use an array to add up the numbers ( int roll[6] )
2) calculate your random answer (do not add 1 giving 0 => 5)
3) use answer as an index into roll[] and increment ( roll[answer]++; )

At the end of the loop, roll[3] will contain the number of 3's "rolled".

I just began learning arrays in programming class, and I have a question for you. Wouldn't

roll[3]

show how many 4's there were? My reasoning behind that is, doesn't it start at zero? Please excuse my ignorance, as I have said, I just began learning arrays. Thank you for your answer!

rfrapp 17 Junior Poster in Training

I'm writing a program that rolls a die a number of times that the user enters, and counts up each time a one, two, three, and so on is rolled. Right now, it only prints out zero's. Any guidance would be appreciated!

Here's my code:

// dice.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
    int six = 0;
    int result[6] = {0};
    int numberOfRolls;
    cout << "Enter the number of times that you want to roll the die: ";
	cin >> numberOfRolls;
    for (int counter= 1; counter < numberOfRolls; counter++)
	{
		
		int answer = result[rand() % 6 + 1];
		if(answer == 1)
			one++;
		if(answer == 2)
			two++;
		if(answer == 3)
			three++;
		if(answer == 4)
			four++;
		if(answer == 5)
			five++;
		if(answer == 6)
			six++;
	}
    cout << "The number of one's rolled is: " << one << endl;
    cout << "The number of two's rolled is: " << two << endl;
    cout << "The number of three's rolled is: " << three << endl;
    cout << "The number of four's rolled is: " << four << endl;
    cout << "The number of five's rolled is: " << five << endl;
    cout << "The number of six's rolled is: " << six << endl;
	system("pause");
	return 0;
}
rfrapp 17 Junior Poster in Training

If you use a FOR loop from 1 to N, this loop would control each line.
Then inside that FOR loop you have more loops that control the pattern of each line.

Your job is to figure out how to create the line pattern. What is the same in each line? What's different? Find the one pattern that defines each line.

Hint: 3 loops...

I see that the square, in this case, is a 9 x 9 square, and that each descending number appears as a multiple of 8 (There are 8 4's, 16 3's, 24 2's, and 32 1's), but i'm not sure of any other patterns. Im sorry, I'm not much of a pattern person. I really do like programming, it's just that the problem solving part is difficult for me. So if you could give me another hint, that'd be nice. I'm not asking you for the answer though. Thank you.

rfrapp 17 Junior Poster in Training

Seemingly, the number has to be between 1 and 9, right?

...but if this is on a quiz, why would you want someone else to answer it?

It was on a quiz that I had already done, and I couldn't figure out the answer. So I'm curious as to what the answer is, or at least I'd like a starting point so that I can find the answer.

rfrapp 17 Junior Poster in Training

I forgot to mention that I'm relatively new to C++, and I have no idea what most of that means :p

rfrapp 17 Junior Poster in Training

This problem is to print a square using a number that the user enters, which would be in the center. If the user entered a 5, then the program should output this:
111111111
122222221
123333321
123444321
123454321
123444321
123333321
122222221
111111111
If you're wondering, this was not a homework assignment. It was on a quiz, and I did not know how to do it. So if someone could give me at the very least some guidance on how to solve this problem, then that'd be nice. I know that I need to use for loops, but I'm not quite sure how. Thank you for your time and assistance.