Thread: Simple Question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    22

    Simple Question

    if I have a for loop like this:

    Code:
    for (i = 0; i < 6; i++)
     {
                    z[i] = a[i]-48;
                    scanf("%d", z[i]);
                                       
     }
     printf("%d", z);
    (1)is there a certain rise why it won't come out and print the array? I am pretty sure it is getting the values but it wont break out and do the print.

    (2)Also is there anyway once I have the entire array Z that I can make it all one integer? Like say I put integer by integer ('9' ,'8', '7') into th array so that the array is ('987') is there a way that I can set the 987 as an integer ninehundred and eighty seven?

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    When using scanf with an integer (z variable array), you must place an amperstand (&) before it. Like this:
    Code:
    scanf("%d", &z[i]);

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    you can't just output a whole array like that (except for strings, and even then you need to use the %s format specifier) you need to step through the array and print out each element one by one
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    I understand that you would have to do it like that but see, I am converting a characters to integers of binary and I need it to be able to read the binary numbers as a sequence of integers obviously. So there is no way that I can put the elements of the array together and assign it to a variable?

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by eater View Post
    I understand that you would have to do it like that but see, I am converting a characters to integers of binary and I need it to be able to read the binary numbers as a sequence of integers obviously. So there is no way that I can put the elements of the array together and assign it to a variable?
    unless it is required by your assignment I would suggest you drop the "char to number that is binary to base 10 equivalent of number that is binary" approach and take a "base 2 to base 10" approach, it's much simpler imo
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Code:
    #include<stdio.h>
    
    int main(void)
    {
            char b[6];
            int z[6];
            int i = 0;
    
            int a= 1;
            int binary;
            int decimal = 0;
            int num;
            int pow = 1;
            int result = 0;
    
    
            printf("Enter a positive binary inteer to be converted (6 bits): ");
            scanf("%s", a);
    
            for (i = 0; i < 6; i++)
            {
                    z[i] = a[i]-48;
                    scanf("%d", &z[i]);
            }
    
    
            while (binary)
            {
                    decimal += binary % 10 * a;
                    a *= 2;
                    binary /= 10;
            }
            printf("Decimal Representation of the Binary Number: %d\n", decimal);
    
            while(decimal > 0)
            {
                    num = decimal % 8;
                    result += num * pow;
                    pow *= 10;
                    decimal /= 8;
            }
            printf("Octal Representation of the Binary Number:  %d\n", result);
    
    }
    See this is my entire program thus far. I need to convert the input string to binary integers and then run it through the conversions of different radixes but its not working out. the conversions to the different radixes are correct but I can't get the input to change to an integer then pass through.

  7. #7
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by eater View Post
    Code:
    #include<stdio.h>
    
    int main(void)
    {
            char b[6];
            int z[6];
            int i = 0;
    
            int a= 1;
            int binary;
            int decimal = 0;
            int num;
            int pow = 1;
            int result = 0;
    
    
            printf("Enter a positive binary inteer to be converted (6 bits): ");
            scanf("%s", a);
    
            for (i = 0; i < 6; i++)
            {
                    z[i] = a[i]-48;
                    scanf("%d", &z[i]);
            }
    
    
            while (binary)
            {
                    decimal += binary % 10 * a;
                    a *= 2;
                    binary /= 10;
            }
            printf("Decimal Representation of the Binary Number: %d\n", decimal);
    
            while(decimal > 0)
            {
                    num = decimal % 8;
                    result += num * pow;
                    pow *= 10;
                    decimal /= 8;
            }
            printf("Octal Representation of the Binary Number:  %d\n", result);
    
    }
    See this is my entire program thus far. I need to convert the input string to binary integers and then run it through the conversions of different radixes but its not working out. the conversions to the different radixes are correct but I can't get the input to change to an integer then pass through.
    break that up into subroutines
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    u mean like into separate functions and calls?

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by eater View Post
    Code:
    #include<stdio.h>
    
    int main(void)
    {
            char b[6];
            int z[6];
            int i = 0;
    
            int a= 1;
            int binary;
            int decimal = 0;
            int num;
            int pow = 1;
            int result = 0;
    
    
            printf("Enter a positive binary inteer to be converted (6 bits): ");
            scanf("%s", a);
    
            for (i = 0; i < 6; i++)
            {
                    z[i] = a[i]-48;
                    scanf("%d", &z[i]);
            }
    
    
            while (binary)
            {
                    decimal += binary % 10 * a;
                    a *= 2;
                    binary /= 10;
            }
            printf("Decimal Representation of the Binary Number: %d\n", decimal);
    
            while(decimal > 0)
            {
                    num = decimal % 8;
                    result += num * pow;
                    pow *= 10;
                    decimal /= 8;
            }
            printf("Octal Representation of the Binary Number:  %d\n", result);
    
    }
    See this is my entire program thus far. I need to convert the input string to binary integers and then run it through the conversions of different radixes but its not working out. the conversions to the different radixes are correct but I can't get the input to change to an integer then pass through.
    u r declaring a as an int and inputting it using %s, also in the for loop u're using it as an array.the condition inside the first while loop looks as if it will result in an infinite loop.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM