Problem with binary strings

P

Pete

Sorry for the ambiguity of my last post, What I am try to do is enter a 10
bit binary string
eg: 1110001010 and then permute them into an array using an array containing
3,5,2,7,4,10,1,9,8,6 as the index, so the 1st bit will be in the P10_out[3],
2nd bit in P10_out[5] and so on. I am sure the problem is to do with the
input string, it is each 1 and 0 of the input is not being read as
individual elements. Your help is much appreciated.

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

main()
{
int P10[10]={3,5,2,7,4,10,1,9,8,6};
int P10_out[10];
int i,index;
int input[10];

printf("\nEnter a 10 Binary Sting: ");
for(i=0; i<10; i++){
scanf("%c", &input);
}
for(i=0; i<10; i++) {
index = P10;
P10_out = input[index-1];
printf("P10_out = %d\n", P10_out);
}
}
I'm trying to a very simple encryption key generator. I have hard coded a 10
binary string into an array, I then want to permute that string using
another array with element of type int for 1 to 10. Then I'm using this
as

You commented out the definition of input above. This code won't
compile.
}*/
for(i=0; i<10; i++) {
index = P10;


When i is 5, index is 10.
P10_out = string[index];


string[10] does not exist. The indices for string run from 0 to 9.
This invokes undefined behavior.
printf("P10_out = %d\n", P10_out);
}
}

As you can see I tyied to enter from the keyboard but the binary string
converted to an int I think


Until you show us your real code, we have no idea what you are talking
about.


<<Remove the del for email>>
 
A

Ash

#include said:
#include<string.h>

main()
{
int P10[10]={3,5,2,7,4,10,1,9,8,6};
int P10_out[10];
int i,index;
int input[10];

printf("\nEnter a 10 Binary Sting: ");
for(i=0; i<10; i++){
scanf("%c", &input);
}
for(i=0; i<10; i++) {
index = P10;
P10_out = input[index-1]; #ifdef XXXX
printf("P10_out = %d\n", P10_out);

#else
printf("P10_out = %c\n", P10_out);
#endif /* XXXX */

The #else part should solve your problem. Also the binary strings that
is input in the program should not contain any spaces eg; 1010101010
and not 1 0 1 0...
What was happening is that input is a array of integers and it is not
initialized, so it contains garbage values. Now in the first for loop
when you are entering input of characters and not integers. Since an
integer element has more space than a character, it contains garbage
values and that is printed out in the next for loop. So instead of
that make the input as character arrays or print only character in the
second for loop by using %c format string

Thanks
Mujoo
 
B

Barry Schwarz

Sorry for the ambiguity of my last post, What I am try to do is enter a 10
bit binary string
eg: 1110001010 and then permute them into an array using an array containing
3,5,2,7,4,10,1,9,8,6 as the index, so the 1st bit will be in the P10_out[3],
2nd bit in P10_out[5] and so on. I am sure the problem is to do with the
input string, it is each 1 and 0 of the input is not being read as
individual elements. Your help is much appreciated.

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

main()
{
int P10[10]={3,5,2,7,4,10,1,9,8,6};
int P10_out[10];
int i,index;
int input[10];

printf("\nEnter a 10 Binary Sting: ");
for(i=0; i<10; i++){
scanf("%c", &input);


input is an int. &input is an int*. %c requires the
corresponding argument to be a char*. More undefined behavior.
}
for(i=0; i<10; i++) {
index = P10;
P10_out = input[index-1];
printf("P10_out = %d\n", P10_out);
}


After you get the input routine to accept char, this will produce
unexpected results. In an ASCII system, if you input a '0', this will
print 48. On an EBCDIC system, it would print 240. If you want to
print out the same value that was input, use the same format.
snip


<<Remove the del for email>>
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top