Thread: read in 1000 numbers at once

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    read in 1000 numbers at once

    i have the following code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char buff[1001];
    
        fgets(buff, sizeof(buff), stdin);
        printf("\n999th character is %c and the 1000th chacter is %c\n the whole thing = %s\n", buff[999], buff[1000], buff);
        return 0;
    }
    but when i paste in the string of 1000 numbers it just quits doesn't do anything ie the printf doesn't run if i force it to pause with a break point and step past the printf i get the wrong result buff[999] is a space and 1000 is 0

    is this doing somthing wierd like trying to convert the integer to a ascii code

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    500
    The 999th character is at offset 998. The offset 1000 is always '\0'. Take a look:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
      char buff[11];
    
      fgets( buff, sizeof buff, stdin );
      printf( "%s\n", buff );
    }
    Code:
    $ gcc -g -o test test.c
    $ gdb -q ./test
    Reading symbols from test...done.
    (gdb) l
    1    #include <stdio.h>
    2    
    3    int main( void )
    4    {
    5      char buff[11];
    6      
    7      fgets( buff, sizeof buff, stdin );
    8      printf( "%s\n", buff );
    9    }
    (gdb) b 7
    Breakpoint 1 at 0x751: file test.c, line 7.
    (gdb) r
    Starting program: .../test 
    
    Breakpoint 1, main () at test.c:7
    7      fgets( buff, sizeof buff, stdin );
    (gdb) n
    1234567890
    8      printf( "%s\n", buff );
    (gdb) x/11c buff
    0x7fffffffda3d:    49 '1'    50 '2'    51 '3'    52 '4'    53 '5'    54 '6'    55 '7'    56 '8'
    0x7fffffffda45:    57 '9'    48 '0'    0 '\000'
    (gdb)

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    god knows what is going on here..... it says that the max number achievable by multiplying 4 adjacent numbers is 6561 (wrong answer) then says that is achieved by multiplying 7, 3, 1 and 6 together (no where near) on stepping through the inner for loop it seems to think there are 4 9's together (there aren't)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int ascii_to_num(int x);
    
    int main()
    {
        int i, j, x, a , b;
        long int answer = 0, temp_answer = 1; //temp_answer has to be 1 rather than 0 as anything * 0 is 0
        char buff[1001];
    
        fgets(buff, sizeof(buff), stdin);// get the whole number as a string
        for (i = 0, j = 3; j < 1000; i++, j++)
        {
            for (x = i; x < j + 1; x++) // get the four digits between i and j
            {
                temp_answer *= ascii_to_num(buff[x]);
            }
            if (temp_answer > answer)
            {
                answer = temp_answer;
                a = i;
                b = j;
            }
            temp_answer = 0;
        }
    
        printf("the largest product produced by 4 adjancent numbers is %ld\nthis is produced with the numbers ", answer);
        for (i = a; i < b + 1; i++)
        {
            printf("%c ", buff[i]);
        }
        printf("\n");
    
        return 0;
    }
    
    int ascii_to_num(int x)
    {
        int result;
    
        switch (x)
        {
            case 48:
                result = 0;
            case 49:
                result =  1;
            case 50:
                result =  2;
            case 51:
                result =  3;
            case 52:
                result =  4;
            case 53:
                result =  5;
            case 54:
                result =  6;
            case 55:
                result =  7;
            case 56:
                result =  8;
            case 57:
                result =  9;
        }
        return result;
    }
    //7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    > for (x = i; x < j + 1; x++) // get the four digits between i and j
    You're not adding together 4 digits.
    j keeps increasing.

    Try
    Code:
    for ( i = 0 ; i < 1000-4 ; i++ ) {
      for ( j = 0 ; j < 4 ; j++ ) {
        temp_answer *= ascii_to_num(buff[i+j]);
      }
    }
    > temp_answer = 0;
    No, you need to reset it back to 1.
    Otherwise all your multiplications end up back at zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    i changed line 17
    Code:
    temp_answer *= ascii_to_num(buff[x]);
    to
    Code:
    temp_answer += ascii_to_num(buff[x]);
    and don't get anywhere near the right answers it thinks 7 + 3 + 1 + 6 = 37 even assuming the above output was generated by multiplying 4 9's together (9*9*9*9) 4 * 9 = 36 not 37

    i must of done something seriously wrong here for the maths processor to be out of kilt.
    coop

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Quote Originally Posted by Salem View Post
    > for (x = i; x < j + 1; x++) // get the four digits between i and j
    You're not adding together 4 digits.
    j keeps increasing.

    Try
    Code:
    for ( i = 0 ; i < 1000-4 ; i++ ) {
      for ( j = 0 ; j < 4 ; j++ ) {
        temp_answer *= ascii_to_num(buff[i+j]);
      }
    }
    > temp_answer = 0;
    No, you need to reset it back to 1.
    Otherwise all your multiplications end up back at zero.
    how does j increase with in the inner loop only x increases surely

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    i = 0 j = 3 (diff of 4)
    x = i
    buff[x] = buff[i]
    x++
    buff[x] = buff[i+1]
    .....
    buff[x] = buff[i+3]
    now increment i and j
    i++, j++
    x = i..........

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    helps when i change the cases for the switch statement from return value to result = value to put a break in each case label duh!! and resetting temp_answer to 0 when i made a comment on the deceleration about setting it to 1 and why is just beyond duh!!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    > for (i = 0, j = 3; j < 1000; i++, j++)
    This is why you add together 4,5,6,7,8,9.... consecutive numbers.

    > temp_answer *= ascii_to_num(buff[x]);
    > temp_answer += ascii_to_num(buff[x]);
    What exactly are you trying to solve?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    Also, ascii_to_num is simply
    Code:
    int ascii_to_num(int x)
    {
        return x - '0';
    }
    The standard guarantees that '0' through '9' are consecutive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    i was just trying to make sure the return of the function was correct rather than getting a big number by 9 ^ 4 expecting 36 is easier to work out in my head

    im obviously missing a fundimental thing here
    [code]
    for (i = 0, j = 3; j < 1000; i ++, j++)
    for (x = i; x < j +1; x++)
    i = 0 j = 3 x = 0
    buff[x]
    x++
    buff[x]
    x++
    buff[x]
    x++
    buff[x]
    x++ wait x = j + 1 break inner loop
    i++ j++
    i = 1 j = 4 x = 1
    .....

  12. #12
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    a simple example of what i am trying to achieve with the for loops
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int i, j, k;
    
        for (i = 0, j = 3; j < 7; i++, j++)
        {
            for (k = i; k < j + 1; k++)
            {
                printf("i = %d j = %d k = %d\n", i, j, k);
            }
            printf("\n");
        }
        return 0;
    }
    produces the output

    Code:
    i = 0 j = 3 k = 0
    i = 0 j = 3 k = 1
    i = 0 j = 3 k = 2
    i = 0 j = 3 k = 3
    
    i = 1 j = 4 k = 1
    i = 1 j = 4 k = 2
    i = 1 j = 4 k = 3
    i = 1 j = 4 k = 4
    
    i = 2 j = 5 k = 2
    i = 2 j = 5 k = 3
    i = 2 j = 5 k = 4
    i = 2 j = 5 k = 5
    
    i = 3 j = 6 k = 3
    i = 3 j = 6 k = 4
    i = 3 j = 6 k = 5
    i = 3 j = 6 k = 6
    ie i and j stay the same while k loops through the numbers from i to j

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    OK, so use that to make your loop in the original program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    umm its what i have except i use x rather than k

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    OK, so instead of reading 1000 chars from a file, test with

    Code:
    char buff[] = "7316717653133";
    int n = strlen(buff);
    for (i = 0, j = 3; j < n; i++, j++) {
      // etc
    }
    Pick some short example string you can easily verify either on paper, or from just studying the debug.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read 2 numbers each and compare them
    By cable in forum C Programming
    Replies: 4
    Last Post: 02-08-2012, 12:12 PM
  2. Replies: 1
    Last Post: 11-04-2011, 01:16 PM
  3. Replies: 11
    Last Post: 06-23-2010, 01:36 AM
  4. how to read numbers in a better way?
    By kcfung in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2006, 03:57 PM
  5. read numbers from a file
    By haroonie in forum C Programming
    Replies: 5
    Last Post: 03-27-2005, 11:30 PM

Tags for this Thread