GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Feb-2005, 19:22
lion123 lion123 is offline
New Member
 
Join Date: Feb 2005
Posts: 6
lion123 is on a distinguished road
Exclamation

Array 1 dimensional help please asap


Hi everyone,

Could someone please help me write a program that displays the following output (the millions of gallons values are given)? We're supposed to use a for loop within a while loop and the frequency table must be generated by a loop. I would appreciate any help I can get.

__________________________________________________ __________
Day no Millions of gallons
__________________________________________________ __________
1 123
2 134
3 122
4 128
5 116
6 96
7 83
8 144
9 143
10 156
11 128
12 138
13 121
14 129
15 117
16 96
17 87
18 148
19 149
20 151
21 129
22 138
23 127
24 126
25 115
26 94
27 83
28 142


__________________________________________________ __________
Sewage per day Frequency of occurrence
__________________________________________________ __________
81- 90 3
91-100 3
101-110 0
111-120 3
121-130 9
131-140 3
141-150 6
151-160 2
Press any key to continue
  #2  
Old 17-Feb-2005, 19:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
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
We love to help. But we need something to help with, since we won't write it for you. Please define better what you need to accomplish. Don't forget to include the code you already have. We'll help you fix it.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 17-Feb-2005, 19:48
lion123 lion123 is offline
New Member
 
Join Date: Feb 2005
Posts: 6
lion123 is on a distinguished road
My problem is not only coding, but also I don't even know how to write the algorithm. So I don't have somewhere to begin except this:

CPP / C++ / C Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()

{
  
   int gallons[gallsize] = {123, 134, 122, 128, 116, 96, 83, 144, 143, 156, 128, 138, 121, 129, 117, 96, 87, 148, 149, 151, 129, 138, 127, 126, 115, 94, 83, 142};

   

   while ()

   {
	 
  
	for ()
	{
		
	}
	


   }
   return 0;  

}
Last edited by LuciWiz : 18-Feb-2005 at 01:43. Reason: Please insert your C code between [c] & [/c] tags
  #4  
Old 18-Feb-2005, 00:12
lion123 lion123 is offline
New Member
 
Join Date: Feb 2005
Posts: 6
lion123 is on a distinguished road
can anyone help me?
  #5  
Old 18-Feb-2005, 07:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by lion123
My problem is not only coding, but also I don't even know how to write the algorithm. So I don't have somewhere to begin except this:

OK that's a start: you have an array of ints that holds the daily sewage amounts (lots of sludge there).

Suppose the assignment was to print out the daily amounts. Would you know how to do this? Well, you could have 28 cout<< statements, one for each day, or you could use a loop. Just to get us to the next step, here's my program to do that. If you compile and run it what do you get?

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

using namespace std;

int main()
{
  const int GALLSIZE = 28;
  
  int gallons[GALLSIZE] = {123, 134, 122, 128, 116, 
                            96,  83, 144, 143, 156,
                           128, 138, 121, 129, 117, 
                            96,  87, 148, 149, 151, 
                           129, 138, 127, 126, 115,
                            94,  83, 142};

  int i;

  for (i = 0; i < GALLSIZE; i++) {
    cout << "gallons[" << i << "] = " << gallons[i] << endl;
  }

  return 0;
}

Now, do you understand what your assignment is? It seems that you want to print the following:

1. The number of days that the amount of sewage was greater than or equal to 81 and less than or equal to 90.

2. The number of days that the amound of sewage was greater than or equal to 91 and less than or equal to 100.

3. Etc.

Now, you already know how to have a loop that looks at all of the daily amounts (and prints them out). Let's suppose that we want to count the number of days that the sewage amount is greater than 92 and less than or equal to 97. How would you do it?

I would create a variable, say bin9297.
I would set its initial value to zero.
As I stepped through the loop I would see if the amount for that day was >90 and also <= 95. If this is true, I would increment the variable bin9295.

If you have been given this assignment, I assume you have had relational operators (greater than, less than, less than or equal to, ...). You must have had if() statements. If you have had boolean operators ("||", "&&", etc.) Here's a way to do this.

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

using namespace std;

int main()
{
  const int GALLSIZE = 28;
  
  int gallons[GALLSIZE] = {123, 134, 122, 128, 116, 
                            96,  83, 144, 143, 156,
                           128, 138, 121, 129, 117, 
                            96,  87, 148, 149, 151, 
                           129, 138, 127, 126, 115,
                            94,  83, 142};

  int i;
  int bin9297 = 0;

  for (i = 0; i < GALLSIZE; i++) {
    //cout << "gallons[" << i << "] = " << gallons[i] << endl;
    if ((gallons[i] > 92) && (gallons[i]) <= 97) {
      bin9297++;
    }
  }

  cout << "Days with amount greater than 92 and less than or equal to 97: " 
       << bin9297 << endl;


  return 0;
}

So: you can make counters for each range of sewage that you want to keep track of. You can make a single loop with if statements that increments the appropriate counter for each day. Then print out the results in the required format.

Regards,

Dave
  #6  
Old 18-Feb-2005, 08:38
lion123 lion123 is offline
New Member
 
Join Date: Feb 2005
Posts: 6
lion123 is on a distinguished road
Thanks a lot Dave,

So far I have the following and it works. My only problem is that I just found out that my prof doesn't want 8 different if statements, she thinks its ineffecient to being wrong (which I totally disagree). She also wants a loop to generate the table. Do you or anyone else have any ideas?

Thanks again

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
  const int GALLSIZE = 28;
  
  int gallons[GALLSIZE] = {123, 134, 122, 128, 116, 
                            96,  83, 144, 143, 156,
                           128, 138, 121, 129, 117, 
                            96,  87, 148, 149, 151, 
                           129, 138, 127, 126, 115,
                            94,  83, 142};

  int i;
  int bin8190 = 0;
  int bin91100 = 0;
  int bin101110 = 0;
  int bin111120 = 0;
  int bin121130 = 0;                             
  int bin131140 = 0;                             
  int bin141150 = 0;                            
  int bin151160 = 0;                             


  for (i = 0; i < GALLSIZE; i++) {
    
    if ((gallons[i] > 81) && (gallons[i]) <= 90) 
	{
      bin8190++;
    }

	if ((gallons[i] > 91) && (gallons[i]) <= 100) 
	{
      bin91100++;
    }

	if ((gallons[i] > 101) && (gallons[i]) <= 110) 
	{
      bin101110++;
    }

	if ((gallons[i] > 111) && (gallons[i]) <= 120) 
	{
      bin111120++;
    }

	if ((gallons[i] > 121) && (gallons[i]) <= 130) 
	{
      bin121130++;
    }

	if ((gallons[i] > 131) && (gallons[i]) <= 140) 
	{
      bin131140++;
    }

	if ((gallons[i] > 141) && (gallons[i]) <= 150) 
	{
      bin141150++;
    }

	if ((gallons[i] > 151) && (gallons[i]) <= 160) 
	{
      bin151160++;
    }


  }

  cout << "State College Sewage Disposal"<<endl;
  cout << "____________________________________________________________"<< endl;
  cout << "Day no                  Millions of gallons                 "<< endl;
  cout << "____________________________________________________________"<< endl;
  cout << endl;


  cout << " 81-90 " << setw(19) << bin8190 << endl;
  cout << " 91-100 " << setw(18) << bin91100 << endl;
  cout << " 101-110 " << setw(17) << bin101110 << endl;
  cout << " 111-120 " << setw(17) << bin111120 << endl;
  cout << " 121-130 " << setw(17) << bin121130 << endl;
  cout << " 131-140 " << setw(17) << bin131140 << endl;
  cout << " 141-150 " << setw(17) << bin141150 << endl;
  cout << " 151-160 " << setw(17) << bin151160 << endl;

       


  return 0;
}
  #7  
Old 18-Feb-2005, 10:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by lion123
Thanks a lot Dave,

So far I have the following and it works. My only problem is that I just found out that my prof doesn't want 8 different if statements, she thinks its ineffecient to being wrong (which I totally disagree). She also wants a loop to generate the table. Do you or anyone else have any ideas?

Thanks again


Well, looking at your code, it seems to do what you told us about.
Actually your tests are not quite right. My example was an example, not a template for an exact implementation.

For example, in your program, all of your tests like this:
CPP / C++ / C Code:
 if ((gallons[i] > 81) && (gallons[i]) <= 90) 

should be something like this:
CPP / C++ / C Code:
 if ((gallons[i] >= 81) && (gallons[i]) <= 90) 

(You missed a couple of counts.)



If the prof has some other requirements, that's pretty much between you and her. (As WaltP suggested, maybe you can give us a little more information regarding the precise requirements. Also, we have no way of knowing what you have covered in class that you are expected to use. You obviously know how to put a program together, given a hint or two.)

As for 8 different if statements: If you are going to test for eight different conditions with if (something), there will be eight somethings.

Instead of separate if() statements, you could use something like this (if -else)
CPP / C++ / C Code:
    if (gallons[i] > 81)  {
      if (gallons[i] <= 90) {
        bin8190++;
      }
      else if (gallons[i] <= 100){
        bin91100++;
      }
      else if (.....)
    }

That's eight nested statements. May be more efficient (?), but lots harder to keep track of (for me at least).

Now, if you are required to have a loop to print out the count values, then I suppose that the count values should be an array:

CPP / C++ / C Code:
int bins[8] = {0, 0, 0, 0, 0, 0, 0, 0};

...
...
  if (test for value between 81 and 90) {
    bin[0]++
  }
  else if (test for value between 91 and 100) {
    bin[1]++
  else if (...
   .... increment bin[something]

...



Now, how do you print the values in a loop:

CPP / C++ / C Code:
  for (i = 0; i < 8; i++) {
   cout << bin[i] << endl;
  }

But now, the output just has the count value for each bin. How do you get the labelled output using a loop? I guess you could have an array of strings that is initialized to the text for each bin.

You see, I can't possibly guess what your prof has in mind. How much of this stuff have you had in class (strings, arrays of strings, etc.???)


Actually, there is a simple mathematical relation between the number of gallons and the index of the array of bins that could eliminate the requirement for eight if-else conditions and also eliminate the requirement for arrays of strings for the labelled output. More efficient? Maybe. More elegant? Maybe. Easier to understand? Maybe. What the prof had in mind? Who knows (not me).

Regards,

Dave
  #8  
Old 18-Feb-2005, 11:25
lion123 lion123 is offline
New Member
 
Join Date: Feb 2005
Posts: 6
lion123 is on a distinguished road
Something was mentioned about using a for loop within a while loop. We don't know anything about strings, this is our first lab after learning what an array is.


My prof writes:

Some student errors that have showed up already -
Your code should not consist of 8 separate if statements:
if( XXX )
XXXXX;
if( XXX )
XXXXX;
if( XXX )
XXXXX;
if( XXX )
XXXXX;
if( XXX )
XXXXX;
if( XXX )
XXXXX;
if( XXX )
XXXXX;
if( XXX )
XXXXX;
This is inefficient to the point of being wrong -
also not considered 'respectable' code.
You can do a lot better - rethink the design.

And the table should be generated by a loop, not
just 8 separate cout statements:
cout << "81 - 90" << XXXX
cout << "91 - 100" << XXXX
cout << "101 - 110" << XXXX
......
Instead, actually calculate the upper and lower bounds
for each pass through the loop.
One pass of the loop should display one row of the table.



right now this is what my program looks like:

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
  const int GALLSIZE = 28;
  
  int gallons[GALLSIZE] = {123, 134, 122, 128, 116, 
                            96,  83, 144, 143, 156,
                           128, 138, 121, 129, 117, 
                            96,  87, 148, 149, 151, 
                           129, 138, 127, 126, 115,
                            94,  83, 142};

  int bin[8] = {0, 0, 0, 0, 0, 0, 0, 0};

  int i, j=9, k=81;
  int bin8190 = 0;
  int bin91100 = 0;
  int bin101110 = 0;
  int bin111120 = 0;
  int bin121130 = 0;                             
  int bin131140 = 0;                             
  int bin141150 = 0;                            
  int bin151160 = 0;                             


  for (i = 0; i < GALLSIZE; i++) {
    
    if ((gallons[i] >= 81) && (gallons[i]) <= 90) 
	{
      bin8190++;
	  bin[0]++;
    }

	if ((gallons[i] >= 91) && (gallons[i]) <= 100) 
	{
      bin91100++;
	  bin[1]++;
    }

	if ((gallons[i] >= 101) && (gallons[i]) <= 110) 
	{
      bin101110++;
	  bin[2]++;
    }

	if ((gallons[i] >= 111) && (gallons[i]) <= 120) 
	{
      bin111120++;
	  bin[3]++;
    }

	if ((gallons[i] >= 121) && (gallons[i]) <= 130) 
	{
      bin121130++;
	  bin[4]++;
    }

	if ((gallons[i] >= 131) && (gallons[i]) <= 140) 
	{
      bin131140++;
	  bin[5]++;
    }

	if ((gallons[i] >= 141) && (gallons[i]) <= 150) 
	{
      bin141150++;
	  bin[6]++;
    }

	if ((gallons[i] >= 151) && (gallons[i]) <= 160) 
	{
      bin151160++;
	  bin[7]++;
    }


  }

  cout << "__________________________________________________" << setw(4) << "__________" << endl;
  cout << "Sewage per day       Frequency of occurrence" << endl;
  cout << "__________________________________________________" << setw(4) << "__________" << endl;

  for (i = 0; i < 8; i++) 
  {
   cout << k << "-" << k+j << setw(20) << bin[i] << endl;
   k=k+j+1;
  }

  cout << endl;

  return 0;
}
  #9  
Old 18-Feb-2005, 16:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by lion123
Something was mentioned about using a for loop within a while loop.
This is inefficient to the point of being wrong -
also not considered 'respectable' code.
You can do a lot better - rethink the design.

And the table should be generated by a loop, not
just 8 separate cout statements:
cout << "81 - 90" << XXXX
cout << "91 - 100" << XXXX
cout << "101 - 110" << XXXX
......
Instead, actually calculate the upper and lower bounds
for each pass through the loop.
One pass of the loop should display one row of the table.

OK: make a loop that calculates the number of days within each bin. You go through this loop eight times. (Call this the "outer loop".)

Before entering the outer loop, set lower and upper limits to 81 and 90.

Each time through the outer loop, you must go through the entire array of gallon[] to count the number of days that the gallon[] value is between lower and upper. (That is, you create an "inner loop" to count the number of days between the limits)

After you leave the inner loop each time, you can print out the count value. Then add 10 to lower and upper limits and continue the outer loop. Here is some pseudo-code for this:

Code:
set lower = 81 set upper = 90 loop for eight bins (outer loop) count = 0 loop for all 28 values in array gallon[] (inner loop) if gallon[i] >= lower and gallon[i] <= upper then increment count end of inner loop cout << lower << "-" << upper << ": " << count << endl; lower += 10 upper += 10 end of outer loop;

Note that this requires exactly as many comparisons as separate if() statements (namely 28 * 8) since you go through the outer loop 8 times and for each time through the outer loop you go through the inner loop 28 times.

There is no way that you should tell your instructor that it's generally more efficient to go through the data as few times as possible (think of a database so large that it can't be held in memory all at once, so multiple passes through the data require many disk or other external memory accesses), and her idea of inefficient is no more inefficient than the method that she considers efficient, but file it away for your own personal future consideration.

I have worked on systems where, if an employee came up with a plan that required multiple passes through a database where one pass could do the trick, he would have been mocked and derided until he ran out of the room sobbing.

I have also worked in places where the boss had ideas that we were required to implement because of some emotional attachment to a method he had used for years, and weren't very efficient. We did it because that's how you get along in the world. No harm was done as far as product integrity was concerned, and specifications were met, so we let the boss be the boss. That is to say we implemented the inefficient algorithm as directed. (But we were really glad to see him go, when he got his promotion --- Free at last! Free at last! Thank you very much! Free at last!)

Regards,

Dave
Last edited by davekw7x : 18-Feb-2005 at 17:17.
  #10  
Old 18-Feb-2005, 18:08
lion123 lion123 is offline
New Member
 
Join Date: Feb 2005
Posts: 6
lion123 is on a distinguished road
Thank You So Much For Your Help I Got Everything Done The Way She Wants It. I Also Learned So Much From This. Thank You So Much.

Have A Great Weekend
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

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
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Function and Array (w/ reference variables) question brookeville C++ Forum 15 07-Dec-2004 01:11
more array help (simulation project..) dilmv C++ Forum 6 17-Oct-2004 07:51
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 21:52

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

All times are GMT -6. The time now is 04:49.


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