My loop gets display output is 0!

Im currently doing a school project and it requires me to find the top three power consumption of selected household from jun to december. Which means i have to use 6 arrays information. Now,ny objective is to get highest and lowest. to represent the Top , Mid and low.

this is my code.
for (j= 0; j < 1; j++)
{
if (powerConsumptionJun[j]< largest)
largest = powerConsumptionJun[j];
if (powerConsumptionJun[j]>smallest)
smallest = powerConsumptionJun[j];
}

the Largest display zero but my smallest will display 143(Base on my Array). Im so frustrated. I look at the tutorial , follow the same way but nothing seems working.. Hope to get your help.
Your logic is backwards. If the next element is GREATER than largest, then you have a new largest.

You need to make sure you initialize "smallest" to a number at least as large as your smallest value. You can arbitrarily pick a very large number, set the value to the max value for an integer (or whatever number type powerConsumtionJun is), or you can pre-assign the value to the first element of the array. If negative values are allowed, then you will need to do something similar for "largest".
@doug4

May i have the example code of what you are trying to say?
closed account (E0p9LyTq)
For LARGEST:

Not this: if (powerConsumptionJun[j] < largest)

This: if (powerConsumptionJun[j] > largest)

Or this: if (largest < powerConsumptionJun[j])

For SMALLEST:

Not this: if (powerConsumptionJun[j] > smallest)

This: if (powerConsumptionJun[j] < smallest)

Or this: if (smallest > powerConsumptionJun[j])
Last edited on
@FurryGuy @doug4

Thank you! it works like a charm! Silly me for not spotting my mistakes. Appreciate it. One more thing, so now, i would like to find 3 values. The Top1, Top2,and top3. It shows now the largest and biggest only. How can i find the top2?

The code below works but it shows the largest and smallest. Now i want to have top3. High,mid and low. Any help?



for (j = 0; j < 1; j++)
{
// June
if (powerConsumptionJun[j] > top1)
top1 = powerConsumptionJun[j];
if (powerConsumptionJun[j] > top2)
top2 = powerConsumptionJun[j];
if (powerConsumptionJun[j] < top3)
top3 = powerConsumptionJun[j];
// August
if (powerConsumptionAug[j] > top1)
top1 = powerConsumptionAug[j];
if (powerConsumptionAug[j] > top2)
top2 = powerConsumptionAug[j];
if (powerConsumptionAug[j] < top3)
top3 = powerConsumptionAug[j];
// July
if (powerConsumptionJuly[j] > top1)
top1 = powerConsumptionJuly[j];
if (powerConsumptionJuly[j] > top2)
top2 = powerConsumptionJuly[j];
if (powerConsumptionJuly[j] < top3)
top3 = powerConsumptionJuly[j];
}
First of all, let's back up for a moment. I want to ask you about your array(s). What are you trying to store in your array(s)?

Right now it seems that you have 3 arrays (powerConsumptionJun, powerConsumptionAug and powerConsumptionJuly), and you look through each of them 1 time. That's essentially the same as having 3 non-array variables.

There are so many ways to do this better. I don't know what you have learned so far and what you are allowed to use in your assignment, but below are a few options.

One option would be a 12-element array powerConsumption[12] in which each element represents a specific month of the year. If you need to have the same information for a number of households, you could use a 2-dimensional array powerConsumption[NUM_HOUSEHOLDS][12]. Then you could loop through each household and check the month you need.

Rather than using 2-dimensional array, you could create a struct that contains 12 variables (or 7 if you only need Jun - Dec) like so:
1
2
3
4
5
6
7
8
struct ConsumptionByMonth
{
   double jun;
   double jul;
   double aug;
   ...
   double dec;
};


Then you could have a 1-dimensional array of these structs:

ConsumptionByMonth powerConsumption[NUM_HOUSEHOLDS]

Then loop through this array an pull out the value you need

powerConsumption[i].jun;

Better still than using arrays is to use a std::vector. For your simple program (I'm making some assumptions here) it will probably not matter a whole lot, but as you get more advanced in your programming, using standard template library (STL) classes will save you headaches and countless hours tracking down memory leaks. It's best to learn to use the STL as early as you can.

Now to your question. What you did will work if you also adjust top2 and top3 when you set top1. (When you set top1, the old top1 and top2 shift down to top2 and top3.) You might want to consider creating an array of top values rather than having individual variables here. too.

What I would do would be use a std::set or some other ordered data structure. As I read each value, I would throw it into the structure, and the largest values would be the 1st 3 in the structure when I was done. Or simpler yet, just throw all the values into a std::vector class (or array) and sort it. The first (or last) 3 elements will be the 3 largest.
Last edited on
this is the modified and correct code.
for (j= 0; j < 1; j++)
{
if (powerConsumptionJun[j]> largest)
largest = powerConsumptionJun[j];
if (powerConsumptionJun[j]<smallest)
smallest = powerConsumptionJun[j];
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/195730/
http://www.cplusplus.com/forum/general/195734/
Last edited on
Topic archived. No new replies allowed.