/***
* Author: Omar IRAQI
*/
#include <stdio.h>
#include <stdlib.h>
#define N 11
int main(void) {
int i, *p, encoded_message[] = {1634558290, 544104804, 1701994827, 539782501, 1918985572,
1970565920, 1953391972, 1226845811, 1936289056, 1870209139, 8565};
char *message;
printf("%s\n", (char*)encoded_message);
/**
* Let's say it again!
*/
message = (char*)malloc(N * sizeof(int));
p = (int*)message;
for (i=0; i < N; i++, p++)
*p = encoded_message[i];
printf("%s\n", message);
return 0;
}
this outputs the message twice:
Ramadan Kareem, dear students. I miss you!
I was wondering what these encoded numbers are since they don't match with ASCII code
Each int should be split into 4 bytes to recover the individual ascii codes. You could simply print each int as hex.
You can also calculate:
1634558290 % 256
(1634558290 >> 8) % 256
(1634558290 >> 16) % 256
and so on.
You have 11 x 4 byte integers for a total of 44 bytes. This corresponds closely to the length of the message.
1 634 558 290 = 0x616D6152
52 : R
61 : a
6D : m
61 : a
Lookup little endian vs big endian for why the bytes are inverted.
Related
Hi i'm fairly new to c but for a program I'm writing I need to convert binary strings to decimal numbers. here is my current code:
int BinaryToInt(char *binaryString)
{
int decimal = 0;
int len = strlen(binaryString);
for(int i = 0; i < len; i++)
{
if(binaryString[i] == '1')
decimal += 2^((len - 1) - i);
printf("i is %i and dec is %i and the char is %c but the length is %i\n", i, decimal, binaryString[i], len);
}
return decimal;
}
int main(int argc, char **argv)
{
printf("%i", BinaryToInt("10000000"));
}
and here is the output:
i is 0 and dec is 5 and the char is 1 but the length is 8
i is 1 and dec is 5 and the char is 0 but the length is 8
i is 2 and dec is 5 and the char is 0 but the length is 8
i is 3 and dec is 5 and the char is 0 but the length is 8
i is 4 and dec is 5 and the char is 0 but the length is 8
i is 5 and dec is 5 and the char is 0 but the length is 8
i is 6 and dec is 5 and the char is 0 but the length is 8
i is 7 and dec is 5 and the char is 0 but the length is 8
5
I'm confused as to why this doesn't work, all help is greatly appreciated. Thanks in advance!
Ps: I'm used to java so at the moment C just makes me cry
The ^ operator is not for exponentiation, but is instead the bitwise XOR operator.
If you want to raise a number to a power of 2, use the left shift operator << to shift the value 1 by the exponent in question.
decimal += 1 << ((len - 1) - i);
The trick is the same as with any number base: for each incoming digit, multiply the accumulator by the number base and add the digit.
#include <stdio.h>
#include <string.h>
int BinaryToInt(char *binaryString)
{
int decimal = 0;
int len = strlen(binaryString);
for(int i = 0; i < len; i++) {
decimal = decimal * 2 + binaryString[i] - '0';
}
return decimal;
}
int main(void)
{
printf("%d", BinaryToInt("10000000"));
return 0;
}
Program output:
128
I need to write a program which prints the conversion table from feet and inches to centimetres. The numbers printed in row i (counting from zero), column j (counting from zero) of the table should be the cm equivalent of i feet and j inches. i should go from 0 to 7, and j from 0 to 11. Each column should be five characters wide, and the cm figures should be rounded to the nearest integer.
The example of required output is given below:
0 3 5 8 10 13
30 33 36 38 41
61 64 66 69 71
91 94 97 99 102
The code I have prints only one row of inches and column of feet but I don't know how to make into table without producing lots of irrelevant repetitions.
The code is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j;
int cm,p;
for (i=0; i<= 11; i++) {
cm =round(i * 2.54);
printf ("%5d",cm);
}
for (j=0; j<=7; j++) {
p =round(j* 12.0 * 2.54);
printf ("%5d\n",p);
}
return 0;
}
This produces:
0 3 5 8 10 13 15 18 20 23 25 28 0
30
61
91
122
152
183
213
What am I doing wrong?
You have one loop after the other. What you need to do is run through the inches loop every iteration of your feet loop. What you get is nested loops:
#include <stdio.h>
int main()
{
for (int feet = 0; feet <= 7; ++feet) {
for (int inches = 0; inches < 12; ++inches) {
int microns = (feet * 12 + inches) * 25400;
int rounded_cm = (microns + 5000) / 10000;
printf("%5d", rounded_cm);
}
puts("");
}
}
I've made some other changes in my version; you're encouraged to study it and understand why it does what it does (read the man page for puts(), for example). Don't just copy it and hand it in - it will be obvious it isn't your code.
An alternative approach is to use a single loop (in inches), and insert a newline when we reach the 11th inch in each foot:
#include <stdio.h>
int main()
{
for (int i = 0; i < 96; ++i) {
printf("%4d%s",
(i * 25400 + 5000) / 10000,
i%12==11 ? "\n" : " ");
}
}
(You'll want to give meaningful names to your constants; the above is written in a "code-golf" style).
Whatever you do, don't be tempted to avoid multiplying by instead adding 2.54 repeatedly in the loop. Floating-point numbers are not exact, and addition will accumulate the error.
OP needs to put the "inches" loop inside the "foot" loop as well answered by others. #Toby Speight #VHS
Code could do its "round to nearest" via the printf() statement by using "%5.0f" to control the output width and rounding.
Let code use foot/inch instead of i/j #KevinDTimm for clarity.
#include <stdio.h>
#define INCH_PER_FOOT 12
#define CM_PER_INCH 2.54
int main(void) {
// go from 0 to 7, and ...
for (int foot = 0; foot <= 7; foot++) {
// from 0 to 11
// for (int inch = 0; inch < INCH_PER_FOOT; inch++) { is more idiomatic
for (int inch = 0; inch <= 11; inch++) {
printf("%5.0f", (foot * INCH_PER_FOOT + inch) * CM_PER_INCH);
}
puts("");
}
}
Output
0 3 5 8 10 13 15 18 20 23 25 28
...
213 216 218 221 224 226 229 231 234 236 239 241
You are running your loops backwards. First you need to run through feet and then through inches. But you are having it the other way round. Check the following snipped and compare it with your code and try to understand what's wrong.
#include <stdio.h>
#include <stdlib.h>
#include <math.h> // for rounding of a number
int main()
{
int i,j;
int cm,p;
for(i=0; i<=7;i++) {
for(j=0;j<=11;j++) {
cm = round(i*30.48 + j*2.54);
printf ("%5d",cm);
}
printf("\n");
}
return 0;
}
So I have this code that randomly generates an integer array based on user input, and puts the elements in ascending and descending order. However, currently, the code only prints the descending order twice. So I would like to know how to make a copy of the array ascd and use the copy in the piece of code that organizes the descending order. I am just a beginner, so I apologize if this is a silly question, and appreciate all the guidance I can get. Here is my code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int main (){
int x;
printf("Enter the size of your array\n");//User is entering number of elements
scanf("%d", &x);
int ascd[x]; //Array
int c;
int d;
int e;
int kk = 0;
int temp;
int tempother;
int turtle;
for(c = 0; c<x; c++){//Randomly generating elements
srand(time(0));
ascd[kk] = (rand() %100) + 1;
}
for(c = 0; c<x; c++){ //Ascending order
for(d = 0; d<(x-c-1); d++){
if(ascd[d] > ascd[d+1]){
temp = ascd[d];
ascd[d] = ascd[d+1];
ascd[d+1] = temp;
}
}
}
for(turtle = 0; turtle<x; turtle++){//Descending order
for(e = 0; e<(x-turtle-1); e++){
if(ascd[e] < ascd[e+1]){
tempother = ascd[e];
ascd[e] = ascd[e+1];
ascd[e+1] = tempother;
}
}
}
printf("The ascending order is\n\n");
for(c = 0; c<x; c++){
printf("%d\n", ascd[c]);
}
printf("\n\nThe descending order is\n\n");
for(turtle = 0; turtle<x; turtle++){
printf("%d\n", ascd[turtle]);
}
}
There are a number of additional issues you need to consider. First always, always, validate user input. If nothing else, with the scanf family of functions, make sure the expected number of conversions were successfully performed. e.g.
int x = 0;
printf ("\n enter the number of elements for your array: ");
if (scanf ("%d", &x) != 1) { /* always validate user input */
fprintf (stderr, "error: invalid input, integer required.\n");
return 1;
}
int ascd[x], desc[x];
Next, you only need to seed the random number generator once. Move srand (time (NULL)); out of the loop.
While not a requirement, it is good practice to initialize your VLA's to all zero (or some number, since you cannot provide an initializer) to eliminate the chance of an inadvertent read from an uninitialized value when iterating over the array (you can consider your filling in this case an initialization, making the memset optional here, but you won't immediately loop and fill in all cases. Something as simple as the following is sufficient if you are not immediately filling the array, e.g.
memset (ascd, 0, x * sizeof *ascd); /* good idea to zero your VLA */
After filling your array, a simple memcpy will duplicate the array if you wish to preserve both ascending and descending sorts, e.g.
for (int i = 0; i < x; i++) /* x random values 1 - 100 */
ascd[i] = (rand () % 100) + 1;
memcpy (desc, ascd, x * sizeof *ascd); /* copy ascd to desc */
The remainder is just a bit of cleanup. Resist the urge to create a (variable next) for every value in your code. That quickly becomes unreadable. While I prefer the C89 declarations, the C99/C11 declarations inside the for block are convenient, e.g.:
for (int i = 0; i < x; i++) /* ascending order */
for (int j = 0; j < (x - i - 1); j++)
if (ascd[j] > ascd[j + 1]) {
int temp = ascd[j];
ascd[j] = ascd[j + 1];
ascd[j + 1] = temp;
}
Putting all the pieces together, and noting that main() is type int and therefore will return a value, you could tidy things up as follows. Your style is completely up to you, but the goal should be readability. e.g.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main (void) {
int x = 0;
printf ("\n enter the number of elements for your array: ");
if (scanf ("%d", &x) != 1) { /* always validate user input */
fprintf (stderr, "error: invalid input, integer required.\n");
return 1;
}
int ascd[x], desc[x];
srand (time (NULL)); /* you only need do this once */
memset (ascd, 0, x * sizeof *ascd); /* good idea to zero your VLA */
for (int i = 0; i < x; i++) /* x random values 1 - 100 */
ascd[i] = (rand () % 100) + 1;
memcpy (desc, ascd, x * sizeof *ascd); /* copy ascd to desc */
for (int i = 0; i < x; i++) /* ascending order */
for (int j = 0; j < (x - i - 1); j++)
if (ascd[j] > ascd[j + 1]) {
int temp = ascd[j];
ascd[j] = ascd[j + 1];
ascd[j + 1] = temp;
}
for (int i = 0; i < x; i++) /* descending order */
for (int j = 0; j < (x - i - 1); j++)
if (desc[j] < desc[j + 1]) {
int temp = desc[j];
desc[j] = desc[j + 1];
desc[j + 1] = temp;
}
printf ("\n the ascending order is\n\n");
for (int i = 0; i < x; i++) {
if (i && !(i % 10)) putchar ('\n');
printf (" %3d", ascd[i]);
}
printf ("\n\n the descending order is\n\n");
for (int i = 0; i < x; i++) {
if (i && !(i % 10)) putchar ('\n');
printf (" %3d", desc[i]);
}
putchar ('\n');
return 0;
}
Example Use/Output
$ ./bin/sort_copy
enter the number of elements for your array: 100
the ascending order is
1 1 4 4 5 5 7 8 8 9
10 13 16 16 17 20 22 22 22 23
24 24 25 27 29 29 33 35 35 35
37 38 40 41 41 41 41 42 44 45
46 48 48 48 49 50 53 54 56 57
58 59 61 61 63 64 65 65 66 66
67 68 68 70 71 73 74 74 74 75
76 80 80 80 80 82 84 84 85 85
85 85 86 88 88 89 89 90 91 91
91 92 92 93 93 93 96 99 100 100
the descending order is
100 100 99 96 93 93 93 92 92 91
91 91 90 89 89 88 88 86 85 85
85 85 84 84 82 80 80 80 80 76
75 74 74 74 73 71 70 68 68 67
66 66 65 65 64 63 61 61 59 58
57 56 54 53 50 49 48 48 48 46
45 44 42 41 41 41 41 40 38 37
35 35 35 33 29 29 27 25 24 24
23 22 22 22 20 17 16 16 13 10
9 8 8 7 5 5 4 4 1 1
Look things over and let me know if you have any questions.
Sorting with qsort
Continuing for the comment, qsort is an optimized sorting routine that is part of the C standard library (in stdlib.h) and is the go-to sort function regardless of the type of data you have to sort. The only requirement that generally catches new C programmers is the need to write a comparison function to pass to qsort so that qsort knows how you want the collection of objects sorted. qsort will compare two elements by passing a pointer to the values to the compare function you write. The declaration for the comparison is the same regardless of what you are sorting, e.g.
int compare (const void *a, const void *b);
You know you are sorting integer values, so all you need to do to sort ascending is to write a function that returns a positive value if a > b, returns zero if they are equal, and finally returns a negative value if b > a. The simple way, is to write
int compare (const void *a, const void *b) {
int x = *(int *)a;
int y = *(int *)b;
return x - y;
}
That satisfies the sort requirement for ascending order, and to sort in descending order return y - x; -- but there is a problem. If x and y happen to be large positive and large negative values, there is a potential that x - y will exceed the maximum (or minimum) value for an integer (e.g. overflow, because the result will not fit in an integer value).
The solution is simple. You can perform the same comparison, but using the results of an inequality, e.g. returning (a > b) - (a < b) for the ascending comparison, and (a < b) - (a > b) for the descending comparison. (this scheme will work for all numeric types, you just need to adjust the cast). Step though the inequality. In the ascending case, if a > b the return is 1 (e.g. return 1 - 0;). If they are equal, the inequality returns 0 (0 - 0 ), and finally if a < b, the value returned is -1 ( 0 - 1 ).
While you are free to continue to explicitly declare the x and y variables, you will generally see it written with the cast in the comparison, eliminating the need for the x and y variables altogether, e.g.
/* integer comparison ascending (prevents overflow) */
int cmpascd (const void *a, const void *b)
{
/* (a > b) - (a < b) */
return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b);
}
Putting those pieces together, the same program can be written using qsort instead of the inefficient nested loops (and moving the print array routine to a function of its own) as follows,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ROW 10
int cmpascd (const void *a, const void *b);
int cmpdesc (const void *a, const void *b);
void prnarr (int *a, int n, int row);
int main (void) {
int x = 0;
printf ("\n enter the number of elements for your array: ");
if (scanf ("%d", &x) != 1) { /* always validate user input */
fprintf (stderr, "error: invalid input, integer required.\n");
return 1;
}
int ascd[x], desc[x];
srand (time (NULL)); /* you only need do this once */
memset (ascd, 0, x * sizeof *ascd); /* good idea to zero your VLA */
for (int i = 0; i < x; i++) /* x random values 1 - 100 */
ascd[i] = (rand () % 100) + 1;
memcpy (desc, ascd, x * sizeof *ascd); /* copy ascd to desc */
qsort (ascd, x, sizeof *ascd, cmpascd); /* qsort ascending */
qsort (desc, x, sizeof *desc, cmpdesc); /* qsort descending */
printf ("\n the ascending order is\n\n");
prnarr (ascd, x, ROW);
printf ("\n\n the descending order is\n\n");
prnarr (desc, x, ROW);
putchar ('\n');
return 0;
}
/* integer comparison ascending (prevents overflow) */
int cmpascd (const void *a, const void *b)
{
/* (a > b) - (a < b) */
return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b);
}
/* integer comparison descending */
int cmpdesc (const void *a, const void *b)
{
/* (a < b) - (a > b) */
return (*(int *)a < *(int *)b) - (*(int *)a > *(int *)b);
}
void prnarr (int *a, int n, int row)
{
for (int i = 0; i < n; i++) {
printf (" %3d", a[i]);
if (i && !((i + 1) % row))
putchar ('\n');
}
}
As with the first answer, give it a try and let me know if you have any questions. (And remember to always compile with a minimum -Wall -Wextra to enable most compiler warnings -- and fix any warnings generated before you consider your code reliable -- you won't run into any circumstance where warnings can be understood and safely ignored anytime soon) Add -pedantic to see virtually all warnings that can be generated. (if you look up pedantic in Websters, you will see why that name is apt.) Just FYI, the gcc compiler string I used to compile the code was:
$ gcc -Wall -Wextra -pedantic -std=c11 -Ofast -o bin/sort_copy sort_copy.c
You print the final array twice, you can have your ascending array output by printing the values of the array right after you have done the ascending operation.
Create an another array to store descending items. You can reverse the ascending array to create the descending array. Try this code.
#include
#include
#include
#include
int main (){
int x;
printf("Enter the size of your array\n");//User is entering number of elements
scanf("%d", &x);
int ascd[x]; //Array
int desc[x];
int c;
int d;
int e;
int kk = 0;
int temp;
int tempother;
int turtle;
int z=0;
for(c = 0; c<x; c++){//Randomly generating elements
srand(time(0));
ascd[kk] = (rand() %100) + 1;
}
for(c = 0; c<x; c++){ //Ascending order
for(d = 0; d<(x-c-1); d++){
if(ascd[d] > ascd[d+1]){
temp = ascd[d];
ascd[d] = ascd[d+1];
ascd[d+1] = temp;
}
}
}
for(turtle = x-1; turtle>=0; turtle--){//Descending order
desc[z]=ascd[turtle];
z++;
}
printf("The ascending order is\n\n");
for(c = 0; c<x; c++){
printf("%d\n", ascd[c]);
}
printf("\n\nThe descending order is\n\n");
for(turtle = 0; turtle<x; turtle++){
printf("%d\n", desc[turtle]);
}
}
I have a problem with a simple code. i want to have all products and sums of a specific number for EX 4 -> 0+4 1+3 2+2 3+1 4+0. for the sum so I made this code to do it:
#include <stdio.h>
#include <stdlib.h>
typedef struct{
unsigned int x;
unsigned int y;
}str;
const int MAX = 200000;
int main(int argc,char *argv[]){
int s=atoi(argv[1]);
if(s<0 || s>MAX){
printf("errore nummero incompatibile!\n");
exit(-1);
}
str sum;
int n=MAX;
for (sum.x=0;sum.x<n;sum.x++){
for (sum.y=0;sum.y<n;sum.y++){
if(strcmp(argv[2],"+")==0){
if(sum.x+sum.y==s){
printf("%d + %d = %d\n",sum.x,sum.y,s);
}
}else{
if(sum.x*sum.y==s){
printf("%d * %d = %d\n",sum.x,sum.y,s);
}
}
}
}
exit(-1);
}
the argv[1] is the number to testand the argv[2] is the mode (Sum or product)
here is the product output of 44 *:
1 * 44 = 44
2 * 22 = 44
4 * 11 = 44
11 * 4 = 44
22 * 2 = 44
44 * 1 = 44
25266 * 169990 = 44
33998 * 126330 = 44
42110 * 101994 = 44
50532 * 84995 = 44
50997 * 84220 = 44
63165 * 67996 = 44
67996 * 63165 = 44
84220 * 50997 = 44
84995 * 50532 = 44
101994 * 42110 = 44
126330 * 33998 = 44
167378 * 179622 = 44
169990 * 25266 = 44
179622 * 167378 = 44`
it gives the correct output but then it starts giving more numbers. these are the same each time i run it. what is this and how can i stop this?
You're iterating through every number until MAX, causing you overflows along the way (See vitaut's answer for the explaination of your issue and how to prevent overflow in your case, he explained that very well.). That's not necessary. When you try to find each combinaisons of multiplication of 2 integer, you just have to iterate until said number, or MAX if the number is too large.
Try to change
int n=MAX;
by :
int n = s;
if (s > MAX)
int n=MAX;
This is caused by integer overflow:
25266 * 169990 == 4294967340
4294967340 is too large to be represented as unsigned int which is 32-bit on your platform. So the most significant bits that don't fit get discarded, effectively giving you the result modulo 2 to the power of 32:
4294967340 mod 2**32 == 44
You can detect overflow in sum.x + sum.y by checking if sum.x > UINT_MAX - sum.y and either leave the inner loop or do something else. Similar check can be done for multiplication.
I want to convert an array of hexadecimal numbers to their corresponding ascii character?
For eg:
arr_hex[] = {6,1,6,2,6,5,6,A,7,A}
to
arr_ascii[] = {a,b,e,j,z}
#include <stdio.h>
#define A 10
#define B 11
#define C 12
#define D 13
#define E 14
#define F 15
int main(void){
int arr_hex[] = {6,1,6,2,6,5,6,A,7,A};
int size = sizeof(arr_hex)/sizeof(*arr_hex);
char arr_ascii[size/2];
int i, j;
for(j=i=0; j < size/2; i+=2){
printf("%c", arr_ascii[j++] = arr_hex[i]*16 + arr_hex[i+1]);
}
printf("\n");
return 0;
}