Thread: Array outputing garbage

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Array outputing garbage

    Hello!

    I'm new to C and am working on a problem I was hoping to get some help. I am trying to create a survey asking a group of people how they liked a new product on a scale of x to y (these must be defined as Macros and must be able to accept negative numbers). The program outputs garbage values for my negative values instead of 0's. I'm sure it is an easy fix? I would love constructive criticism on my code as a whole too. Thanks. Code is as follows...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_RESPONSE_VALUE 5
    #define MIN_RESPONSE_VALUE -5
    #define MAX_RESPONDENTS 5
    #define ERROR_EXIT 3
    
    
    int main(void)
    {
       int responsevalue[ MAX_RESPONSE_VALUE - MIN_RESPONSE_VALUE + 1 ] = {0};
       int temp = 0;
       int errorcounter = 0;
       int usercounter = 1;
       int printloop;
    
       do
       {
          printf("PERSON NUMBER %d, How did you like our new product?\n", usercounter);
          printf("Please enter a number on a scale of %d to %d,\n", MIN_RESPONSE_VALUE, MAX_RESPONSE_VALUE);
          printf("Where %d is \"I Hated It!\" and %d is \"I Loved It!\"\n", MIN_RESPONSE_VALUE, MAX_RESPONSE_VALUE);
          printf("(To exit program early, Please enter %d Out Of Range responses.)\n: ", ERROR_EXIT);
          scanf("%d", &temp);
          printf("\nThank you.\n\n");
       
          if (temp >= MIN_RESPONSE_VALUE && temp <= MAX_RESPONSE_VALUE)
          {
             responsevalue[ temp - 1 ]++;
             usercounter++;
          }
          else
          {
             printf("Error: Value out of range.  Try Again.\n\n");
             errorcounter++;
          }
       }
       while (errorcounter < ERROR_EXIT && usercounter <= MAX_RESPONDENTS);
       
          printf("Here are your results!\n\nRATING     # OF RESPONSES\n\n");
          for(printloop = MIN_RESPONSE_VALUE - 1; printloop < MAX_RESPONSE_VALUE; printloop++)
             printf("%d           %d\n", printloop + 1, responsevalue[printloop]);
          
    
       return(EXIT_SUCCESS);
    }

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    No wonder. Do you know it's not allowed to use negative numbers as array indexes?
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    responsevalue[ temp - 1 ]++;
    There. Make it temp + 5.

    MIN_RESPONSE_VALUE - 1
    Same.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Here's an example where you can use negative index using pointer.
    Code:
    int a[5]={1,2,3,4,5};
    int *p;
    p=&a[2];
    printf("%d",p[-1]); // will print 2
    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

  5. #5
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    using pointer
    Yes, pointer, since it is merely translated to *(p - 1). Do you want to raise a discussion about esoteric trivia, or we'd rather stick to good programming style?

    Btw, 1[p - 2] is perfectly valid too.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    I think it is more of you spoke in absolutes, thus that has a HIGH probability that you are wrong.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by GL.Sam View Post
    Do you want to raise a discussion about esoteric trivia, or we'd rather stick to good programming style?
    The equivalence between a[b] and *(a+b) is not "esoteric trivia" any more than Einstein's equivalence of E = mc^2 is esoteric trivia. It is a critical link between arrays and pointers, and in my experience, it's the piece of information that finally "clicks" the understanding of pointers for a lot of people.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Okay, let's rewind it to starting point. First off, when I say a word I'm not trying to make anyone believe it like undoubtedly truth in its last occurence. Perhaps, I should've added clear "In general" to my first sentence. Before anything else I'm trying to help a novice, bearing that intention in mind, I assume that usually there is no need of stating special cases. Topic starter haven't posed a question about relation between arrays and pointers, am I right? In fact, there were no pointer occurences at all. I think he just looked forward for receiving just-at-the-moment help.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I was just giving an example of negative array indices, it's a different fact that it's a good programming style or not, but it's valid too.
    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

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    #define MAX_RESPONSE_VALUE 5
    #define MIN_RESPONSE_VALUE -5
    
       int responsevalue[ MAX_RESPONSE_VALUE - MIN_RESPONSE_VALUE + 1 ] = {0};
    Okay, the above declares an array of 11 elements, so only indexes 0 to 10 are valid. With me so far?
    Code:
          for(printloop = MIN_RESPONSE_VALUE - 1; printloop < MAX_RESPONSE_VALUE; printloop++)
             printf("%d           %d\n", printloop + 1, responsevalue[printloop]);
    This for-loop iterates over indexes -6 to 4, and those indexes are used in the array lookup - UH OH!
    See the problem? It's called buffer underrun. The fix ... well IMHO the below is probably what you are after:
    Code:
          for(printloop = MIN_RESPONSE_VALUE; printloop <= MAX_RESPONSE_VALUE; printloop++)
             printf("%d           %d\n", printloop, responsevalue[printloop - MIN_RESPONSE_VALUE]);
    The for loop iterates over -5 to 5 inclusive, and it access array elements 0 to 10

    Quote Originally Posted by Gl.Sam
    There. Make it temp + 5.
    Sounds like you're suggesting hardcoding a 5 in there which would mean that changing the value of the #define wouldn't quite work any more. The correct change for that line is:
    Code:
    responsevalue[temp - MIN_RESPONSE_VALUE]++;
    which as you can see mirrors the above fix to the printf in the for loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Thank you Imalc

    Worked like a charm!

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by BEN10 View Post
    Here's an example where you can use negative index using pointer.
    Code:
    int a[5]={1,2,3,4,5};
    int *p;
    p=&a[2];
    printf("%d",p[-1]); // will print 2
    In the declaration

    p = &a[2] is the & sign not redundant. The arrays are supposed to decay into pointers.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    a[2] is the contents of the memory adress a+2
    &a[2] is the memory address

    ie a[2] is equivelent to *(a+2)
    &a[2] is equivelent to a+2

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    In the declaration

    p = &a[2] is the & sign not redundant. The arrays are supposed to decay into pointers.
    Just imagine, p is a pointer, so it holds address of any variable, and a[2] is the value of the 3rd element, so it cant be p=a[2].(actually it could be, but while doing *p will create undefined behaviour). But in my example it cant be.
    Last edited by BEN10; 08-05-2009 at 09:47 PM.
    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. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Outputing an array to a textbox ?
    By C of Green in forum C# Programming
    Replies: 2
    Last Post: 10-31-2006, 11:28 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

Tags for this Thread