Thread: My while looping is pooping. Exercise 12 in chapter 12

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    My while looping is pooping. Exercise 12 in chapter 12

    Hi Guys, This chapter on pointers and arrays is definitely challenging me. I took advice from this forum and purchased a book on C and am working through the exercises. Basically, my while loop won't finish even though the condition hasn't been met to exit.

    First, I get compile warnings which I can't figure out how to resolve:
    Code:
    ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
    C:\work\~\chapt-12-ex12.c||In function 'find_two_largest':|
    warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]|
    warning: assignment to 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]|
    warning: assignment to 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]|
    ||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 1 second(s)) ===|
    Here's the code:
    Code:
    #include <stdio.h>
    //C Programming - a modern approach, 2nd edition by K.N. King
    // IMPORTANT: must use pointer arithmetic, not subscription
    
    
    void find_two_largest(const int *a, int n, int *largest, int *second_largest);
    
    
    int main(void){
        int b[] = {5,4,3,2,1,8,6,9,7};
        int num = (sizeof(b)/sizeof(b[0]));
        int val1=999, val2=999;
        int *lgst=&val1, *slgst=&val2;
        find_two_largest(b, num, lgst, slgst);
        printf("in main: largest = %d and 2nd largest = %d\n",*lgst, *slgst);
    }
    
    
    void find_two_largest(const int *a, int n, int *largest, int *second_largest){
        *largest = a[0];
        int *last = &a[n-1];
        //printf("last = %d\n",last);   // last prints mem location. *last prints value (7)
        *second_largest = a[0];
        //while(a<&a[n]){               // can't use as both a values increase due to a++
        while(a<=last){                 // compares memory location
            printf("1st in while: a = %d and last = %d\n",a, last); //prints memory locations
            printf("before if *largest = %d and *a = %d\n",*largest, *a);
            if(*largest<*a){            // compares actual values
                printf("if success!\n");
                second_largest=*largest;
                largest=*a;
                printf("in if: largest = %d and second_largest = %d\n",largest, second_largest);
                a++;    // this is working as expected 11/25 @ 6AM
            }else{
                printf("if failed!\n");
                a++;
            }
        }
        return;
    }
    Here's what I see on the CLI:
    <<< I CAN'T SEEM TO ADD AN IMAGE>>>

    Code:
    1st in while: a = 6421984 and last = 6422016
    before if *largest = 5 and *a = 5
    if failed!
    1st in while: a = 6421988 and last = 6422016
    before if *largest = 5 and *a = 4
    if failed!
    1st in while: a = 6421992 and last = 6422016
    before if *largest = 5 and *a = 3
    if failed!
    1st in while: a = 6421996 and last = 6422016
    before if *largest = 5 and *a = 2
    if failed!
    1st in while: a = 6422000 and last = 6422016
    before if *largest = 5 and *a = 1
    if failed!
    1st in while: a = 6422004 and last = 6422016
    before if *largest = 5 and *a = 8
    if success!
    in if: largest = 8 and second_largest = 5
    1st in while: a = 6422008 and last = 6422016
    
    
    Process returned -1073741819 (0xC0000005)   execution time : 2.794 s
    Press any key to continue.
    Notice on the last line of out put before "Process returned" the line is:
    Code:
    1st in while: a = 6422008 and last = 6422016.
    6422008 is below 6422016 which means it should have kept looping instead of exiting.

    The above is why I can't just 'printf' my way to figuring out what I did wrong. Thank you for your insight.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int *last = &a[n-1];
    Make this
    const int *last = &a[n-1];

    > while(a<=last)
    This should be <, not <=

    > second_largest=*largest;
    > largest=*a;
    You want to update what is pointed to.
    *second_largest=*largest;
    *largest=*a;
    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.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Thank you Salem. That all worked.
    Learning pointers has been tough for me, especially with arrays. I try to use logic to figure it out but, sometimes it comes down to random attempts to try to get it to work. I tried adding * then removing *. Both *largest=*a and largest=*a were both giving the same printf result which is why random trying wasn't helping this time. I'll keep at it. I'm grateful for your help...again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2016, 05:50 AM
  2. Replies: 3
    Last Post: 08-04-2016, 05:35 AM
  3. Jumping into C++ Chapter 14, exercise 6.
    By CppProgrammer88 in forum C++ Programming
    Replies: 5
    Last Post: 04-12-2016, 07:27 PM
  4. Alex's exercise Chapter 6 Ex3
    By Valentas in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2013, 10:55 AM

Tags for this Thread