C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-04-2009, 06:46 PM   #1
Registered User
 
Join Date: Aug 2009
Posts: 30
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);
}
steals10304 is offline   Reply With Quote
Old 08-04-2009, 06:54 PM   #2
Registered User
 
GL.Sam's Avatar
 
Join Date: Aug 2009
Posts: 39
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
GL.Sam is offline   Reply With Quote
Old 08-04-2009, 07:00 PM   #3
Registered User
 
GL.Sam's Avatar
 
Join Date: Aug 2009
Posts: 39
Quote:
responsevalue[ temp - 1 ]++;
There. Make it temp + 5.

Quote:
MIN_RESPONSE_VALUE - 1
Same.
__________________
The only good is knowledge and the only evil is ignorance.
~Socrates
GL.Sam is offline   Reply With Quote
Old 08-04-2009, 09:22 PM   #4
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
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.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 08-04-2009, 09:59 PM   #5
Registered User
 
GL.Sam's Avatar
 
Join Date: Aug 2009
Posts: 39
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
GL.Sam is offline   Reply With Quote
Old 08-04-2009, 10:07 PM   #6
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
I think it is more of you spoke in absolutes, thus that has a HIGH probability that you are wrong.
Kennedy is offline   Reply With Quote
Old 08-04-2009, 10:23 PM   #7
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 08-04-2009, 10:41 PM   #8
Registered User
 
GL.Sam's Avatar
 
Join Date: Aug 2009
Posts: 39
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
GL.Sam is offline   Reply With Quote
Old 08-05-2009, 12:26 AM   #9
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
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.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 08-05-2009, 03:41 AM   #10
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
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
iMalc is offline   Reply With Quote
Old 08-05-2009, 11:10 AM   #11
Registered User
 
Join Date: Aug 2009
Posts: 30
Thank you Imalc

Worked like a charm!
steals10304 is offline   Reply With Quote
Old 08-05-2009, 01:59 PM   #12
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 300
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.
roaan is offline   Reply With Quote
Old 08-05-2009, 02:08 PM   #13
Registered User
 
Join Date: Jun 2009
Posts: 150
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
KBriggs is offline   Reply With Quote
Old 08-05-2009, 09:44 PM   #14
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
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.
__________________
HOPE YOU UNDERSTAND.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition

Last edited by BEN10; 08-05-2009 at 09:47 PM.
BEN10 is offline   Reply With Quote
Reply

Tags
array, garbage, noob

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
from 2D array to 1D array cfdprogrammer C Programming 17 03-24-2009 10:33 AM
Outputing an array to a textbox ? C of Green C# Programming 2 10-31-2006 11:28 AM
Unknown Memory Leak in Init() Function CodeHacker Windows Programming 3 07-09-2004 09:54 AM
Quick question about SIGSEGV Cikotic C Programming 30 07-01-2004 07:48 PM
Array Program emmx C Programming 3 08-31-2003 12:44 AM


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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