C programing code

Joined
Aug 1, 2023
Messages
4
Reaction score
0
Dear friend, I created a program in my college to guess your number by binary system first number displayed 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 is your number in the above list ask if yes first number here 1 add if no 1 is not added second 2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31 then again ask is your number in the above list ask if yes first number here 2 add if no 2 is not added third time again 4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31 then again ask is your number in the above list ask if yes first number here 4 add if no 4 is not added fourth time 8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31 then again ask is your number in the above list ask if yes first number here 8 add if no 8 is not added and last time means fifth time on-screen display 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 then again ask is your number in the above list ask if yes first number here 16 add if no 16 is not added

this program is based on the binary unit base which I created in 2005 in college time by using only if function and loop function even I ask for chatgpt and google bard but code is not working can anyone help me to creating this code
 
Joined
Aug 1, 2023
Messages
4
Reaction score
0
i write code c code but here use array
#include <stdio.h>

int main() {
printf("Dear friend, I will guess your number using the binary system.\n");
int guess = 0;

// Arrays to hold the numbers for each iteration
int numbers[5][16] = {
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31},
{2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31},
{4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31},
{8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31},
{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}
};

// Loop for each iteration
for (int i = 0; i < 5; i++) {
printf("Is your number in the following list?\n");
for (int j = 0; j < 16; j++) {
printf("%d ", numbers[j]);
}
printf("\nEnter '1' if yes, '0' if no: ");

int answer;
scanf("%d", &answer);

if (answer == 1) {
guess += numbers[0];
}
}

printf("Your number is: %d\n", guess);
return 0;
}

i want number generated by loop not by array
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
very good and funny !

use all formulas you use to build your array and each series number.
create the base formula you build as function, you'll get 5 new function.

A : start : 1 ,then n+2 until 16 members
B : start : 2 ,then every duets are b = a + 1, then 3 is added at the 2nd number.
C : start : 4 ,then for the 3 after it's +1 apply, then to change the group, add 5 to last of the quadret.
D : start : 8 ,n+1 until 8 numbers are in the group. then n+9
E : start : 16 ,then n+1 apply until 31

the head of all arrays are pow(2) members, and assume being part of the final 'guess'.
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
Way too complicated FResher.

Only one simple function is needed.

F(a,b): examine integer a, return true if bit b is set

Keep the i loop as it is.

Change the j loop, from 0 to 31.

Print j if F(j,i)
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
Way too complicated FResher.

Only one simple function is needed.

F(a,b): examine integer a, return true if bit b is set

Keep the i loop as it is.

Change the j loop, from 0 to 31.

Print j if F(j,i)
I was just making the details about current n-dimension arrays in the original code.
Hoping a ( without defined Array ) generator/methods for build all values.

It's a description about "how" was made all rows in the array.

Look closer at each array, you 'll see the logical in.
It's big arrays with sub-groups inside, except the first and the last.


details , that all values to build to achieve without arrays :
C:
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31} n+2

-------------------------------------------------------------
{2, 3 | 6, 7 | 10, 11 | 14, 15 | 18, 19 | 22, 23 | 26, 27 | 30, 31} 8 sub groups n+1,gap n+1+3
-------------------------------------------------------------
{4, 5, 6, 7 | 12, 13, 14, 15 | 20, 21, 22, 23 | 28, 29, 30, 31 } 4 sub groups n+1, gap n+5
-------------------------------------------------------------
{8, 9, 10, 11, 12, 13, 14, 15 | 24, 25, 26, 27, 28, 29, 30, 31 } 2 sub groups n+1, gap 9
-------------------------------------------------------------
{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} n+1
 
Last edited:
Joined
Aug 1, 2023
Messages
4
Reaction score
0
#include <stdio.h>

void printNumbers(int start, int increment, int end) {
while (start <= end) {
for (int i = start; i < start + increment && i <= end; i++) {
printf("%4d ", i);
}
printf("\n");
start += increment * 2;
}
}

int main() {
int number;
printf("Enter a number (1, 2, 4, 8, 16,32,64,128,256): ");
scanf("%d", &number);

int start = number;
int end = 511;

switch (number) {
case 1:
printNumbers(start, 1, end);
break;
case 2:
printNumbers(start, 2, end);
break;
case 4:
printNumbers(start, 4, end);
break;
case 8:
printNumbers(start, 8, end);
break;
case 16:
printNumbers(start, 16, end);
break;
case 32:
printNumbers(start, 32, end);
break;
case 64:
printNumbers(start, 64, end);
break;
case 128:
printNumbers(start, 128, end);
break;

case 256:
printNumbers(start, 256, end);
break;
default:

printf("Invalid number entered.\n");
break;
}

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top