trying to read input into an array

This is a discussion on trying to read input into an array within the C Programming forums, part of the General Programming Boards category; Originally Posted by trprince sorry one other thing. This is working backwards for me. i get a -1 for ascending ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by trprince View Post
    sorry one other thing. This is working backwards for me. i get a -1 for ascending number (1 2 3 4 5). here is the code i have

    Code:
    float ascend_arr(float aarry[], int sizearr){
      int ascend;
      for(ascend = 1; ascend < sizearr;){
        if (aarry[ascend] < aarry[ascend + 1]){
          ascend++;
        } 
        else{
          return 1;
        }
      } 
      return -1; 
    }
    Try to keep one question in one thread, and not discussing the SAME problem in two different threads.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
      int ascend;
      for(ascend = 1; ascend < sizearr;){
        if (aarry[ascend] < aarry[ascend + 1]){
    You start at 1 instead of 0, and you overrun the end of the array with aarry[ascend+1]. Oops. I think you meant
    Code:
    for(ascend = 0; ascend < sizearr-1; ) {
    BTW, pronouncable variable names wouldn't hurt. "aarry" is just painful . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. Read file into array
    By neobarn in forum C Programming
    Replies: 6
    Last Post: 01-09-2009, 11:41 AM
  3. read file list into array
    By deltaxfx in forum C Programming
    Replies: 1
    Last Post: 01-07-2006, 10:41 PM
  4. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  5. function to read an int from input
    By linucksrox in forum C Programming
    Replies: 8
    Last Post: 06-08-2004, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21