GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Nov-2004, 01:08
Nexa Nexa is offline
Awaiting Email Confirmation
 
Join Date: Nov 2004
Location: Florida
Posts: 23
Nexa is on a distinguished road

Using Arrays to Summarize Survey Results


I need help from anyone out there who can help explain why this program gets the results for "Frequency". as it does. I do not understand why it's doing what it's doing. The program runs fine, I just do not understand : frequency[ rating ] and why it's giving me that output. The Rating array makes perfect sense.

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

int main()
{
   // define array sizes
   const int responseSize = 40;   // size of array responses
   const int frequencySize = 11;  // size of array frequency

   // place survey responses in array responses
   int responses[ responseSize ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8,
       10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7,
       5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };

   // initialize frequency counters to 0
   int frequency[ frequencySize ] = { 0 };

   // for each answer, select value of an element of array
   // responses and use that value as subscript in array
   // frequency to determine element to increment
   for ( int answer = 0; answer < responseSize; answer++ )
      ++frequency[ responses[answer] ];

   // display results
   cout << "Rating" << setw( 17 ) << "Frequency" << endl;

   // output frequencies in tabular format
   for ( int rating = 1; rating < frequencySize; rating++ )
      cout << setw( 6 ) << rating
           << setw( 17 ) << frequency[ rating ] << endl;

   return 0;  // indicates successful termination

} // end main
Last edited by Nexa : 29-Nov-2004 at 01:11. Reason: Code error need to fix it
  #2  
Old 29-Nov-2004, 02:43
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
I noticed you initialized the frequency with an element number of frequencysize, but you only gave it one element. Wouldn't you want to initialize it with all the elements, like so?
CPP / C++ / C Code:
int frequency[ frequencySize ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Or not give the elements at all?
  #3  
Old 29-Nov-2004, 02:46
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Well, it's a simple statistical algorithm.
You are right, responses makes perfect sense. You could read it "2 people answered 2, 11 people answered 6..." if you count the "answeres".
There are 10 possiblle responses, so the size of the frequency array is 11 (10+ 1).

Now, lets look at the piece of code I think you are having trouble with:

CPP / C++ / C Code:
  for ( int answer = 0; answer < responseSize; answer++ )
      ++frequency[ responses[answer] ];

First, please observe that answer iterates on the whole responses vector (size = 40 different people interogated).
For every answer in the list, the frequency item at the respective position (1 - 10 possible responses; there are no other values in the responses array) is incremented, thus resulting an array(frequency) with the items at the indices representing the number of responses equal to the index (frequency[1] = how many responses of "1", frequency[2] = how many responses of "2" and so on...)

There you have it. I hope this isn't such a vague explanation as it seems to me
For more questions on this, you are welcome to post back.

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 29-Nov-2004, 02:48
Nexa Nexa is offline
Awaiting Email Confirmation
 
Join Date: Nov 2004
Location: Florida
Posts: 23
Nexa is on a distinguished road
Quote:
Originally Posted by Dr. Evil
I noticed you initialized the frequency with an element number of frequencysize, but you only gave it one element. Wouldn't you want to initialize it with all the elements, like so?
CPP / C++ / C Code:
int frequency[ frequencySize ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Or not give the elements at all?


I'm not really sure Dr. Evil the code is not mine, however I must understand what it is doing in order to finish my assignment.

Are you asking if int frequency[frequencySize] = [0,0,0,0,0,0,0,0,0,0,0]; because it is set to 11?
  #5  
Old 29-Nov-2004, 09:53
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Dr. Evil
I noticed you initialized the frequency with an element number of frequencysize, but you only gave it one element. Wouldn't you want to initialize it with all the elements, like so?
CPP / C++ / C Code:
int frequency[ frequencySize ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Or not give the elements at all?
Actually, if you do not give values to the elements, they will be initialized to garbage. The program will create the array and whatever happens to be in that memory will be the values.

CPP / C++ / C Code:
int frequency[ frequencySize ] = { 0 };
is in fact supposed to zero out the entire array, so this should work fine.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Knight tour (arrays help needed) dilmv CPP / C++ Forum 7 18-Oct-2004 14:31
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 00:22
Trying to retrieve selective results from a table. icsys MySQL / PHP Forum 9 18-Nov-2003 03:31
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 10:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:01.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.