Thread: Arrays in C

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    18

    Arrays in C

    I'm trying to get this program to tell if the numbers in the array are increasing, decreasing or neither. However I can't seem to figure out how to get the program to print neither. Any suggestions?

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    int main() {
    int i;
    int n=0;
    bool decreasing = false;
    bool increasing = false;
    bool neither = false;
    
    
    int x[3]= {1,2,3};
        for(i=x[n]; i<3; i=x[n+=1]){
            if(x[n]>x[n+=1]){
                decreasing = true;
            }
            
            if(x[n]<x[n+=1]){
                increasing = true;
            }
    
    
        }
        if(increasing){
            printf("increasing\n");
        }
        if (decreasing){
            printf("decreasing\n");
        }
        else{
             printf("neither\n");
        }
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Get rid of the code and start from scratch.

    Code:
        for(i=x[n]; i<3; i=x[n+=1]){ // This is wrong
            if(x[n]>x[n+=1]){ // This is wrong
                decreasing = true;
            }
            
            if(x[n]<x[n+=1]){ // This is wrong
                increasing = true;
            }
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    How is it wrong? It compiles and tells if it increasing or decreasing. it just won't tell if it is neither.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Your for loop set the value of "i"; this value is never used inside the loop.
    While this is sometimes correct to do in a for loop; it is NOT for your loop.

    I challenge you to state in words what your for loop does line by line.

    To me the code is random writing from a person who has no idea what they want to do in the code.

    Edit: Change this line
    Code:
    int x[3]= {1,2,3};
    to
    Code:
    int x[3]= {4,5,6};
    Edit2B:
    or

    Code:
    int x[3]= {-10,-9,-8};
    Edit2: And, watch your code crash and burn!

    Tim S.
    Last edited by stahta01; 03-12-2014 at 02:37 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    /*
    ||=== scrap_c, Debug ===|
    main.c||In function 'main':|
    main.c|13|warning: operation on 'n' may be undefined|
    main.c|17|warning: operation on 'n' may be undefined|
    main.c|8|warning: unused variable 'neither'|
    ||=== Build finished: 0 errors, 3 warnings ===|
    */

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    anyway , the use of boolean variable is extremely foreign to C , I also suggest trying to write real C code which does not use this feature.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Dave11 View Post
    anyway , the use of boolean variable is extremely foreign to C , I also suggest trying to write real C code which does not use this feature.
    I disagree with this advice as being valid in general.
    But, it might be valid for this exact problem; without more knowledge of this problem, I can NOT say if bool is a good or bad idea.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    Yeah I'm new to C programming so if it looks like I don't know what I'm doing that's probably because I don't...
    Thanks for your help.

  9. #9
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    What about this for the "for" loop?
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    int main() {
    int i;
    int n=0;
    bool decreasing = false;
    bool increasing = false;
    bool neither = false;
    
    
    int x[3]= {-8,-7,-6};
        for(i=0; i<3; i+=1){
    	    if(x[i]>x[i+=1]){
    		    decreasing = true;
    		}
    		
    	    if(x[i]<x[i+=1]){
    		    increasing = true;
    	    }
    
    
        }

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Same issue:

    Code:
    /*
    main.c||In function 'main':|
    main.c|12|warning: operation on 'i' may be undefined|
    main.c|16|warning: operation on 'i' may be undefined|
    main.c|7|warning: unused variable 'neither'|
    main.c|4|warning: unused variable 'n'|
    ||=== Build finished: 0 errors, 4 warnings ===|
    */
    Code:
    if(x[i]>x[i+=1])
    You should not be modifying the value of 'i' here, as it results in undefined behavior.

    Question 3.1
    Question 3.8

    You can test the value at the index 'i+1' without changing the value of 'i':

    Code:
    if(x[i]>x[i+1])
    However, if you do this, your "for()" loop should be running one less than the maximum number of elements in the array.

    int x[3] has three valid elements: x[0], x[1], and x[2]. So the following code would run your array out of bounds:

    Code:
    for(i=0; i<3; i+=1)
        if(x[i]>x[i+1])
            decreasing = true;
    
    /*
        i = 0 ---> accessing 'x[0]' and 'x[1]'
        i = 1 ---> accessing 'x[1]' and 'x[2]'
        i = 2 ---> accessing 'x[2]' and 'x[3]' <-- oh no!  out of bounds!
    */
    You should try to develop the logic by hand, on paper, before trying to write the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  2. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread