C programming help!

skyver7

Member
Joined
Aug 4, 2006
Messages
198
Reaction score
0
hi

are there any C programming gurus out there? i need help with my assignment urgently.
 

shioeer

Junior Member
Joined
Jan 20, 2011
Messages
3
Reaction score
0
Hello. Need help on programming C++ assignment too urgently.

Looking for tutors or does anybody have any contacts? Thank you!
 

yssmile

Junior Member
Joined
Oct 14, 2015
Messages
8
Reaction score
0
need help on encoding a string

i have a 60 characters input string to convert using a substitution table:
1312e7972714d01a46664a3523366a2f58560b42c485853674561c360d77

Index 0 123456789abcdef
S-box 0 24c9a37e10 f86bd

Example: when input = 1 ,will convert to output = 2;
input a, output = 0

i have write a code as such, but output is totally messed up,need expert to correct my code...thx alot guys

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

main()

{
char input[60] = { "1312e7972714d01a46664a3523366a2f58560b42c485853674561c360d77" };

char sbox[8][16] = {
{ '2', '4', 'c', '5', '9', 'a', '3', '7', 'e', '1', '0', 'f', '8', '6', 'b', 'd' },
{ '7', 'e', 'a', '4', '1', 'd', '9', 'f', '2', 'b', '6', '0', 'c', '5', '8', '3' },
{ '3', '8', 'f', '1', 'a', '6', '5', 'b', 'd', 'e', '4', '2', '7', '0', '9', 'c' },
{ '8', 'f', '7', '9', '3', 'd', '4', 'b', 'c', '2', '0', 'a', '1', '5', '6', 'e' },
{ '1', 'f', '8', '3', 'c', '0', 'b', '6', '2', '5', '4', 'e', '9', 'a', '7', 'd' },
{ '1', 'd', 'f', '0', 'e', '8', '2', 'c', '7', '4', 'b', 'a', '3', '5', '9', '6' },
{ 'a', '3', '4', '7', 'b', '8', 'd', '2', '6', 'c', 'f', 'e', '0', '5', '1', '9' },
{ '3', 'c', '4', '2', '7', 'a', '0', '8', '5', 'b', 'f', '1', 'd', '6', '9', 'e' }
};

int box_id,i;

for (i = 0; i < 60; i++)
{

printf("%c", sbox[0][input]);

}
system("pause");
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
i have a 60 characters input string to convert using a substitution table:
1312e7972714d01a46664a3523366a2f58560b42c485853674561c360d77

Index 0 123456789abcdef
S-box 0 24c9a37e10 f86bd

Example: when input = 1 ,will convert to output = 2;
input a, output = 0

i have write a code as such, but output is totally messed up,need expert to correct my code...thx alot guys

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

main()

{
char input[60] = { "1312e7972714d01a46664a3523366a2f58560b42c485853674561c360d77" };

char sbox[8][16] = {
{ '2', '4', 'c', '5', '9', 'a', '3', '7', 'e', '1', '0', 'f', '8', '6', 'b', 'd' },
{ '7', 'e', 'a', '4', '1', 'd', '9', 'f', '2', 'b', '6', '0', 'c', '5', '8', '3' },
{ '3', '8', 'f', '1', 'a', '6', '5', 'b', 'd', 'e', '4', '2', '7', '0', '9', 'c' },
{ '8', 'f', '7', '9', '3', 'd', '4', 'b', 'c', '2', '0', 'a', '1', '5', '6', 'e' },
{ '1', 'f', '8', '3', 'c', '0', 'b', '6', '2', '5', '4', 'e', '9', 'a', '7', 'd' },
{ '1', 'd', 'f', '0', 'e', '8', '2', 'c', '7', '4', 'b', 'a', '3', '5', '9', '6' },
{ 'a', '3', '4', '7', 'b', '8', 'd', '2', '6', 'c', 'f', 'e', '0', '5', '1', '9' },
{ '3', 'c', '4', '2', '7', 'a', '0', '8', '5', 'b', 'f', '1', 'd', '6', '9', 'e' }
};

int box_id,i;

for (i = 0; i < 60; i++)
{

printf("%c", sbox[0][input]);

}
system("pause");
}

Why are you using a 2D array when all you need is a 1D array. Also you don't need an array. Just a string

Code:
char *sbox = "24c59a37e10f86bd7ea41d9....69e";

for (i = 0; i < sizeof(input)/sizeof(input[0]; i++) {
  printf("%c", sbox[input[i]];
}

Is the code above right ? Wrong, but it demonstrate the idea of how to perform simple substitution.

Your larger issue is looking at the ASCII table, eg: https://upload.wikimedia.org/wikipedia/commons/d/dd/ASCII-Table.svg
you will see a-f and 0-9 is not in one big area.

the ASCII value of input[0] is not 1, it's a character '1' and when you use it as an index, you are using the ASCII value which correspond to 31
so in fact sbox[input[0]] = sbox[31] which might not map to what you think it might be.

For your substitution to work this easily, you need a larger sbox of more than just 16 elements. From my calculation, you will need 122 - 30 + 1 elements with some unused elements, if you do not
want to play with if..else statements.
 

yssmile

Junior Member
Joined
Oct 14, 2015
Messages
8
Reaction score
0
hi ...davidktw, im delighted when seeing ur reply.

just to ans ur question , the 2d array table is actually given in the assignment to substitute in the 60 char of string to generate the output.

im require to generate a random number to fix the row which is the 8 sbox. <<im ok until this part

just now i was doing a try and error using below code and i realise , it gave me output number from sbox[3] instead of sbox[0].


char input={"0123456789"}
char sbox[8][16] = {
{ '2', '4', 'c', '5', '9', 'a', '3', '7', 'e', '1', '0', 'f', '8', '6', 'b', 'd' },
{ '7', 'e', 'a', '4', '1', 'd', '9', 'f', '2', 'b', '6', '0', 'c', '5', '8', '3' },
{ '3', '8', 'f', '1', 'a', '6', '5', 'b', 'd', 'e', '4', '2', '7', '0', '9', 'c' },
{ '8', 'f', '7', '9', '3', 'd', '4', 'b', 'c', '2', '0', 'a', '1', '5', '6', 'e' },
{ '1', 'f', '8', '3', 'c', '0', 'b', '6', '2', '5', '4', 'e', '9', 'a', '7', 'd' },
{ '1', 'd', 'f', '0', 'e', '8', '2', 'c', '7', '4', 'b', 'a', '3', '5', '9', '6' },
{ 'a', '3', '4', '7', 'b', '8', 'd', '2', '6', 'c', 'f', 'e', '0', '5', '1', '9' },
{ '3', 'c', '4', '2', '7', 'a', '0', '8', '5', 'b', 'f', '1', 'd', '6', '9', 'e' }
};

for (i = 0; i < 10; i++)
{ printf("%c", sbox[0][input ]);}



as for the a-f part, im thinking to use if else statement, cos im not really familiar with ascii code, but before entering if else part i got to settle above error first.

sorry, im really new and quite dumb on this , so allow me to debug it step by step. :)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
hi ...davidktw, im delighted when seeing ur reply.

just to ans ur question , the 2d array table is actually given in the assignment to substitute in the 60 char of string to generate the output.

im require to generate a random number to fix the row which is the 8 sbox. <<im ok until this part

just now i was doing a try and error using below code and i realise , it gave me output number from sbox[3] instead of sbox[0].


char input={"0123456789"}
char sbox[8][16] = {
{ '2', '4', 'c', '5', '9', 'a', '3', '7', 'e', '1', '0', 'f', '8', '6', 'b', 'd' },
{ '7', 'e', 'a', '4', '1', 'd', '9', 'f', '2', 'b', '6', '0', 'c', '5', '8', '3' },
{ '3', '8', 'f', '1', 'a', '6', '5', 'b', 'd', 'e', '4', '2', '7', '0', '9', 'c' },
{ '8', 'f', '7', '9', '3', 'd', '4', 'b', 'c', '2', '0', 'a', '1', '5', '6', 'e' },
{ '1', 'f', '8', '3', 'c', '0', 'b', '6', '2', '5', '4', 'e', '9', 'a', '7', 'd' },
{ '1', 'd', 'f', '0', 'e', '8', '2', 'c', '7', '4', 'b', 'a', '3', '5', '9', '6' },
{ 'a', '3', '4', '7', 'b', '8', 'd', '2', '6', 'c', 'f', 'e', '0', '5', '1', '9' },
{ '3', 'c', '4', '2', '7', 'a', '0', '8', '5', 'b', 'f', '1', 'd', '6', '9', 'e' }
};

for (i = 0; i < 10; i++)
{ printf("%c", sbox[0][input ]);}



as for the a-f part, im thinking to use if else statement, cos im not really familiar with ascii code, but before entering if else part i got to settle above error first.

sorry, im really new and quite dumb on this , so allow me to debug it step by step. :)


Actually if you want to use the 2D array also can.

For your case of a 8Rx16C array, you can easily convert from 1D access to 2D access using

Remember division and quotient ?
1DARRAY[X] = 2DARRAY[X / 16][X % 16];

Okay 8*16 is 128 elements, that's the old ASCII table without extended characters

Go online read the ASCII table, you will get a better idea.

Another way is to FORCE cast your 2D into 1D. This is C code, so data typing is at your control :)

Let me give you an idea, using your sbox data,

sbox[1][3] ---> '4'
If I treat the sbox as a 1D array, it is actually 1D_sbox[1*16 + 3] --> 1D_sbox[19] --> '4'

So from C code perspective, I can do EXACTLY this

char *sbox_ptr = (char*)sbox;
sbox_ptr[19] --> sbox[1][3] --> '4'

What I have shown is a bit of pointer arithmetic.
 

yssmile

Junior Member
Joined
Oct 14, 2015
Messages
8
Reaction score
0
hihi...thx for your explaination. :)

just a simple one first ya...
would u mind to help to check y is my code gives me output from sbox[3] when im asking to generate output from sbox[0]?
 

KnightNiwrem

Senior Member
Joined
Jun 1, 2014
Messages
1,057
Reaction score
0
Actually if you want to use the 2D array also can.

For your case of a 8Rx16C array, you can easily convert from 1D access to 2D access using

Remember division and quotient ?
1DARRAY[X] = 2DARRAY[X / 16][X % 16];

Okay 8*16 is 128 elements, that's the old ASCII table without extended characters

Go online read the ASCII table, you will get a better idea.

Another way is to FORCE cast your 2D into 1D. This is C code, so data typing is at your control :)

Let me give you an idea, using your sbox data,

sbox[1][3] ---> '4'
If I treat the sbox as a 1D array, it is actually 1D_sbox[1*16 + 3] --> 1D_sbox[19] --> '4'

So from C code perspective, I can do EXACTLY this

char *sbox_ptr = (char*)sbox;
sbox_ptr[19] --> sbox[1][3] --> '4'

What I have shown is a bit of pointer arithmetic.

Actually, from what I understand, the "real" length of the sbox, for any instance, should be 16. He uses a random number generator to pick 1 out of the 8 available sbox to map a number in base 16 to some encrypted base 16 number.

Therefore, there is no need to map every single char in the unextended ASCII table to his 8x16 sbox.

At least... we can agree that the requirements by TS is very confusing.

Regardless, this would then, simply, be a case of doing:

Code:
int current_index = input[i] - '0'; // if 0-9

OR

int current_index = input[i] - 'a' + 10; // if a-f

THEN

print sbox[random_number_generated][current_index];

The actual proper syntax has been omitted as an exercise for TS.
 
Last edited:

KnightNiwrem

Senior Member
Joined
Jun 1, 2014
Messages
1,057
Reaction score
0
hihi...thx for your explaination. :)

just a simple one first ya...
would u mind to help to check y is my code gives me output from sbox[3] when im asking to generate output from sbox[0]?

David has already given the answer to this question.

Simply, '0' != 0. The ASCII mapping of char to int can be found online. In this case, the char '0' == the int 48. The char '1' == the int 49... and so forth.

Arrays can be imagined as being allocated as a contiguous chunk of memory. Therefore, because '0' is actually 48 as an int, your array access:

sbox[0]['0']

Is actually

sbox[0][48]

And by pointer arithmetic, this is equivalent to

sbox[3][0]
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Actually, from what I understand, the "real" length of the sbox, for any instance, should be 16. He uses a random number generator to pick 1 out of the 8 available sbox to map a number in base 16 to some encrypted base 16 number.

Therefore, there is no need to map every single char in the unextended ASCII table to his 8x16 sbox.

At least... we can agree that the requirementd by TS is very confusing.

Regardless, this would then, simply, be a case of doing:

Code:
int current_index = input[i] - '0'; // if 0-9

OR

int current_index = input[i] - 'a' + 10; // if a-f

THEN

print sbox[random_number_generated][current_index];

The actual proper syntax has been omitted as an exercise for TS.

You are right. But the code doesn't coincide with requirement and hard for me to advise easily. But well TS, was what explained by KnightNiwrem what you need?
 

yssmile

Junior Member
Joined
Oct 14, 2015
Messages
8
Reaction score
0
hihi..knight...really sorry for my bad explanation on my question.
but u have gotten my issue right...and it is working correctly now.


thanks for your detailed step by step explanation ..
Big thanks !!! :D
 

yssmile

Junior Member
Joined
Oct 14, 2015
Messages
8
Reaction score
0
You are right. But the code doesn't coincide with requirement and hard for me to advise easily. But well TS, was what explained by KnightNiwrem what you need?
hi...david...yah...knight had got my idea there...
my bad...sorry to have troubled you much on my explanation....:|

to be frank i was having some hardtime trying to digest on your explanation due to my limited knowledge on C.

but i really happy to see u replying me and trying to help me.
appreciate it!!...:)
 

ykgoh

Master Member
Joined
Jan 1, 2000
Messages
2,782
Reaction score
0
Just curious yssmile is a computer science major?

Or majoring in something else like engineering, mathematics or physics being made to do C programming for the first time?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
hi...david...yah...knight had got my idea there...
my bad...sorry to have troubled you much on my explanation....:|

to be frank i was having some hardtime trying to digest on your explanation due to my limited knowledge on C.

but i really happy to see u replying me and trying to help me.
appreciate it!!...:)


You are welcome, keep trying.
 

yssmile

Junior Member
Joined
Oct 14, 2015
Messages
8
Reaction score
0
Just curious yssmile is a computer science major?

Or majoring in something else like engineering, mathematics or physics being made to do C programming for the first time?
hi...im obviously not from com science major...

im part time student in engineering...:)
 

yssmile

Junior Member
Joined
Oct 14, 2015
Messages
8
Reaction score
0
need help again, struggling in calling out function

Below is my code that just done halfway, I have issue calling out function_1, it just work within the void main part. Can anyone help to t/s ?thx alot in advance!!

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

void function_1(struct library);
void function_2(struct library);
void function_3(struct library);


typedef struct {
char title[60];
char firstname[20];
char lastname[20];
char pubinfo[50];
char category[5];
char index[20];
int pages;
char location[20];
char type[5];
float price;

}library;
/*initialize the 15 books*/
library book[15] = {
{ "Charlie and the Chocolate Factory", "ROALD", "DAHL", "Penguin Hill", "FIC", "C123.45FIC", 80, "P-R", "CIR", 25.99 },
{ "Charlie and the Willy Wonka", "ROALD", "DAHL", "Penguin Hill", "FIC", "C124.55FIC", 73, "P-R", "CIR", 25.99 },
{ "The Power of Focus", "JACK", "CANFILL", "Arbor House", "NFIC", "T234.65NFIC", 257, "A-C", "CIR", 30.50 },
{ "The Power Now", "ECKHART", "TOLLE", "New World", "NFIC", "T237.7NFIC", 300, "A-C", "CIR", 25.55 },
{ "The Monk who Sold His Ferrari", "ROBIN", "SHARMA", "Bison Books", "NFIC", "T234.88NFIC", 63, "P-R", "CIR", 17.88 },
{ "Discovering Statistics", "JAMES", "PATTERSON", "Pollity", "PSY", "P520.14PSY", 952, "J-L", "REF", 138.99 },
{ "Exploring Quantum Mechanics", "MACK", "ERIC", "Orion Books", "SCI", "S620.23SCI", 864, "M-O", "REF", 123.75 },
{ "The Lives of A Cell", "LEWIS", "THOMAS", "Longman", "BIO", "B755.53BIO", 538, "J-L", "REF", 98.65 },
{ "Solid State Theory", "STEVEN", "PALMER", "Parragon", "PHY", "P550.63", 741, "S-U", "REF", 85.29 },
{ "Permutation Groups", "VICTOR", "CAMERON", "Grafton", "MATH", "M863.77MATH", 536, "V-X", "REF", 75.99 },
{ "Natural Food Colorants", "HENDRY", "SCOTT", "Harvest House", "CHEM", "C933.01CHEM", 298, "G-I", "REF", 69.99 },
{ "Engineering Books", "BRUCE", "TURNER", "Heyday Books", "ENGG", "E452.31ENGG", 964, "A-C", "REF", 162.39 },
{ "Bacteria", "OLIVER", "RATNER", "Marshall", "SCI", "S621.33SCI", 349, "M-O", "REF", 78.50 },
{"Corporate Accounting and Reporting", "PICKER", "KIESO", "Wiley", "BUS", "B056.87BUS", 266, "K-M", "REF", 59.80},
{ "Retail Management", "BARRY", "Berman", "Pearson", "BUS", "B069.89BUS", 250, "K-M", "REF", 72.70 },

};



void main()
{

int choice,cont,exit=0;
char a;

do
{
printf("Welcome to SMILE Library!!\n\n");
printf("Search by Author's name -- Pls enter 1\n");
printf("Search by Title ---------- Pls enter 2\n");
printf("Detail search -------------Pls enter 3\n");
scanf_s("%d", &choice);

switch (choice)
{

case 1: function_1; break;
case 2: function_2; break;
case 3: function_3; break;
default:
printf("\n\tERROR!!\nIncorrect menu option.\n");
break;
}

do
{
Sleep(1000);
printf("\nDo you want to continue:\n1.Yes\t\t2.Exit application\n");
scanf_s("%d", &cont);
printf("\n\nProceeding...\n\n");
Sleep(1000);
system("cls");

if (cont != 1 && cont != 2)/*User input out of range*/
{
printf("\nInvalid input, please select again\n");
}
} while (cont != 1 && cont != 2);/*loop back to choose continue or exit*/

}while (cont == 1);/*loop back to menu */

printf("Thanks for using.\n");
printf("Hope to see you again!\n");
Sleep(1500);
return (0);

}

void function_1()
{
int name,i;
char fname, lname;

printf("Author's name\n\n");
printf("Press 1 to search by first name\n");
printf("Press 2 to search by last name\n");
scanf_s("%d\n",&name);

switch (name)
{
case 1:
printf("Pls enter first name of the Author:");
scanf_s("%s", &fname);

for (i = 0; i < 15; i++)
{
if (strcmp(fname, book.firstname) == 0)
{
printf("Title:%s\nAuthor:%s %s\nPublisher:%s\nLocation:%s\nCirculation/Reference:%s\nPrice:%f\n", book.title, book.firstname, book.lastname, book.pubinfo, book.location, book.type, book.price);
}

}
break;

case 2:
printf("Pls enter last name of the Author:");
scanf_s("%s", &lname);

for (i = 0; i < 15; i++)
{
if (strcmp(lname, book.lastname) == 0)
{
printf("Title:%s\nAuthor:%s %s\nPublisher:%s\nLocation:%s\nCirculation/Reference:%s\nPrice:%f\n", book.title, book.firstname, book.lastname, book.pubinfo, book.location, book.type, book.price);
}

}
break;

default:
printf("Incorrect Option! Pls try again.");
break;
}

return;
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Below is my code that just done halfway, I have issue calling out function_1, it just work within the void main part. Can anyone help to t/s ?thx alot in advance!!

Go and read up how you call(or also known technically as "invoke") a function.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top