[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 34 KB, 493x585, angery.jpg [View same] [iqdb] [saucenao] [google]
9468697 No.9468697[DELETED]  [Reply] [Original]

I have a C program that i am working on for a class. Its simple but due to my lack of c knowledge there is only so much i can do. we have to:
Complete a C program that will prompt for and accept two 4-digit hexadecimal numbers and then combine them (i.e., not add them) into a single 32-bit number and print out the results. 1. Prompt for and read in two 4-digit hexadecimal numbers. You may assume that the user enters a hexadecimal number, though it may be out of range. If out of range, you will continually repeat this process until two valid 4-digit hexadecimal numbers are entered. 2. You will print out the decimal representation for each of these hexadecimal numbers. 3. You will concatenate the two 4-digit hexadecimal numbers as follows into a single 8-digit hexadecimal number, where the first hexadecimal number will be in the 4 least significant digits while the second hexadecimal number will be in the 4 most significant digits. 4. You will print out the concatenated hexadecimal number as well as its decimal representation
I got to print out the hex value and decimal value of the two values entered but then i combine them, it just puts the two numbers together rather than merging them into one long number. I have thought of using a string to merge them but i do not know what i would have to do make it a string, ask for user input, and get it to print out the hexadecimal value and decimal value. Making it into a string would make it easier to merge but i just do not knoe how to do that as well as making sure it only allows hexadecimal numbers and not negative numbers or hexadecimal values greater than 4 digits.
Will add code to thread of what i have so far.

>> No.9468699

#include <stdio.h>
#include <stdlib.h>

int main()
{

int hex;
int hex2;

//User enters first hexadecimal and prints the result
printf("Enter a 4-digit Hexadecimal:");
scanf("%x",&hex);
printf("The hexadecimal %x in decimal is %d\n",hex,hex);

//User enters second hexadecimal and prints the result
printf("Enter a 4-digit Hexadecimal:");
scanf("%x",&hex2);
printf("The hexadecimal %x in decimal is %d\n",hex2,hex2);

//Program combines the two numbers and prints the result
printf("The two hexadecimals together make %X%X an\nd its decimal form is %d%d",hex,hex2,hex,hex2);


}

>> No.9469480

>>9468699
What do you mean it puts the numbers together, as in add them, multiply them?
Can you give an example of it by hand how the console should look because your paragraph was bordering gibberish

>> No.9469490

>>9468699
To make it keep asking, you have to create a loop that surrounds the number request, which will keep looping until the entered number is valid

So for instance

While(hex >FFFF)
{
>Request Ur shit here
If (hex >FFFF)
{
>Print "r u fuckin retarded try again"
}
End

It'll keep looping that bit of code until they put something under FFFF in, use ur brain to work out how to make it above zero (hint, look up AND conditions)

>> No.9469503
File: 161 KB, 612x792, BE A WOMAN.jpg [View same] [iqdb] [saucenao] [google]
9469503

>>9468697
#include <stdio.h>
#include <stdlib.h>

int main()
{

unsigned int hex;
unsigned int hex2;

//User enters first hexadecimal and prints the result
printf("Enter a 4-digit Hexadecimal:");
fflush(stdout);
while(1){
scanf("%x",&hex);
printf("The hexadecimal %x in decimal is %u\n",hex,hex);
if(hex > 65535)
printf("The hexadecimal is out of bounds! Please reenter a 4-digit Hexadecimal:"), fflush(stdout);
else break;
}
//User enters second hexadecimal and prints the result
printf("Enter another 4-digit Hexadecimal:");
fflush(stdout);
while(1){
scanf("%x",&hex2);
printf("The 2nd hexadecimal %x in decimal is %u\n",hex2,hex2);
if(hex2 > 65535)
printf("The 2nd hexadecimal is out of bounds! Please reenter a 4-digit Hexadecimal:"), fflush(stdout);
else break;
}

//Program combines the two numbers and prints the result
printf("The two hexadecimals together make %X and its decimal form is %u",(hex2<<16) | hex,(hex2<<16) |hex);
}

>> No.9469508
File: 19 KB, 400x421, 1455081182577.jpg [View same] [iqdb] [saucenao] [google]
9469508

>>9468697
>I have thought of using a string to merge them but i do not know what i would have to do make it a string, ask for user input, and get it to print out the hexadecimal value and decimal value. Making it into a string would make it easier to merge but i just do not knoe how to do that as well as making sure it only allows hexadecimal numbers and not negative numbers or hexadecimal values greater than 4 digits.

>people like this actually exist

>> No.9469513

>>9469490
>While(hex >FFFF)
>{
>>Request Ur shit here
>If (hex >FFFF)

You don't need 2 checks for the same thing. Also you have to prefix hex numbers with 0x in code so it's (hex > 0xFFFF)

>> No.9469585

hi, /g/ here. I'm not gonna do your homework for you, but here's some hints.

>1. Prompt for and read in two 4-digit hexadecimal numbers. You may assume that the user enters a hexadecimal number, though it may be out of range. If out of range, you will continually repeat this process until two valid 4-digit hexadecimal numbers are entered.
Use a do-while loop to do this. essentially

------------------------------------------
long input;
do {
printf([prompt])
input = [get the user input]
}while (input > 0xFFFF)
-------------------------------------------

Suggestion: write a function to do this so you don't copy/paste the code.

>3. You will concatenate the two 4-digit hexadecimal numbers as follows into a single 8-digit hexadecimal number, where the first hexadecimal number will be in the 4 least significant digits while the second hexadecimal number will be in the 4 most significant digits

Use a bit shift and addition.
--------------------------------
int16_t hex1 = 0x5678
int16_t hex2 = 0x1234
int32_t result = hex2; // 32-bit integer holds 8 hex digits; result = 0x00001234
result = result << 16 // shifts the bits 16 spaces to the left; result = 0x12340000
result += hex1; // adds the remaining bits to the empty space; result = 0x12345678
---------------------------------

>I have thought of using a string to merge them but i do not know what i would have to do make it a string, ask for user input, and get it to print out the hexadecimal value and decimal value. Making it into a string would make it easier to merge but i just do not knoe how to do that as well as making sure it only allows hexadecimal numbers and not negative numbers or hexadecimal values greater than 4 digits.
If you do this, I will find you and break your fingers so you can never write such shitty code ever again.

>> No.9469596
File: 76 KB, 528x565, 1456985396416.jpg [View same] [iqdb] [saucenao] [google]
9469596

>>9469585
>addition
>not bitwise OR

>> No.9469602

>>9469596
the compiler will optimize it to something like

movzx eax, hex1
shl eax, 16
movzx ebx, hex2
or eax, ebx

either way desu senpai

>> No.9469620

>>9469602
>relying on the compiler when you just have to hit the right key

>> No.9469632

>>9469620
fair enough