![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 30
| Array outputing garbage 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 | |
| | #2 |
| Registered User 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 | |
| | #3 | ||
| Registered User Join Date: Aug 2009
Posts: 39
| Quote:
Quote:
__________________ The only good is knowledge and the only evil is ignorance. ~Socrates | ||
| GL.Sam is offline | |
| | #4 |
| DESTINY 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 | |
| | #5 |
| Registered User 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 | |
| | #6 |
| Jaxom's & Imriel's Dad 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 | |
| | #7 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| 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 | |
| | #8 |
| Registered User 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 | |
| | #9 |
| DESTINY 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 | |
| | #10 | |
| Algorithm Dissector 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};
Code: for(printloop = MIN_RESPONSE_VALUE - 1; printloop < MAX_RESPONSE_VALUE; printloop++)
printf("%d %d\n", printloop + 1, responsevalue[printloop]);
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]);
Quote:
Code: responsevalue[temp - MIN_RESPONSE_VALUE]++;
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger | |
| iMalc is offline | |
| | #11 |
| Registered User Join Date: Aug 2009
Posts: 30
| Thank you Imalc Worked like a charm! |
| steals10304 is offline | |
| | #12 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 300
| |
| roaan is offline | |
| | #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 | |
| | #14 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| 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 | |
![]() |
| Tags |
| array, garbage, noob |
| Thread Tools | |
| Display Modes | |
|
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 |