4 Replies - 1447 Views - Last Post: 11 April 2010 - 03:36 AM Rate Topic: -----

#1 xdeathcorex  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-March 10

problem in displaying highest and lowest

Posted 10 April 2010 - 10:38 AM

so here's part of my code. here i want to display the highest and lowest values of struct data.


int highQty = 0, lowQty = 0, highPrice = 0, lowPrice = 0;
	x = readFile(); // returns number of items in inventory
	
	// calculate highest and lowest quantity item
	// calculate highest and lowest price
	
	highQty = inventory[1].intrQty;
	lowQty = inventory[1].intrQty;
	highPrice = inventory[1].perPrice;
	lowPrice = inventory[1].perPrice;
	
	for ( i = 1; i <= x; i++)
	{
		if ( inventory[i].intrQty > highQty )
		highQty = i;
		if ( inventory[i].intrQty < lowQty )
		lowQty = i;
		if ( inventory[i].perPrice > highPrice )
		highPrice = i;
		if ( inventory[i].perPrice < lowPrice )
		lowPrice = i;
	}


here's the structure

struct data{
	int intrQty; // will store quantity values
	double perPrice; // will store the price per item
}inventory[100];


the problem is, after i compile and run it, the highest and lowest values did not work instead of giving false output.
for example, the highest in quantity in inventory is 540 but it displays the highest 43 (the last quantity of item in inventory).

so i think that's the part of the code i need to show you guys. if anything else unclear pls tell me.

so how can i make it work?

Is This A Good Question/Topic? 0
  • +

Replies To: problem in displaying highest and lowest

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: problem in displaying highest and lowest

Posted 10 April 2010 - 10:41 AM

First of all, arrays in C++ *should* be indexed from 0 -> N - 1. I assume that you indexed your array struct from 1 -> N. No problems, but you may end up in trouble. Anyway, take a look here:
if ( inventory[i].intrQty > highQty )
   highQty = i;


You are assigning the INDEX of the maximum element to highQty, not the maximum element. You should try this instead:
if (inventory[i].intrQty > highQty)
   highQty = inventory[i].intrQty; // now we assigned the maximum element, not the index


You have to modify the rest aswell.
PS: for efficiency sake, you should start to loop from 2 -> N, since at the first iteration, neither of the "ifs" will execute. It's just checking the first element with the first element.

This post has been edited by sarmanu: 10 April 2010 - 10:49 AM

Was This Post Helpful? 3
  • +
  • -

#3 xdeathcorex  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-March 10

Re: problem in displaying highest and lowest

Posted 10 April 2010 - 10:53 AM

hmm the reason i assign the index is because, after the for loops, there's output statements which outputs all the info in struct. for example

cout << "Name : " << inventory[highQty].itemName;
cout << "Highest quantity is : " << inventory[highQty].intrQty;
and so on..


so i think it is easy to assign it as the index of the inventory array.
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: problem in displaying highest and lowest

Posted 10 April 2010 - 11:01 AM

You just have to think about a little bit:
if ( inventory[i].intrQty > highQty )
    highQty = i;


You assign the index to highQty, right? Then, at the next iteration, you check if inventory[i].intrQty it's bigger THAN INDEX!!! (because highQty is the index). So, you have to find a way to track the maximum/minimum value too. My above example is an easier way to determine what you need.

This post has been edited by sarmanu: 10 April 2010 - 11:05 AM

Was This Post Helpful? 2
  • +
  • -

#5 xdeathcorex  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-March 10

Re: problem in displaying highest and lowest

Posted 11 April 2010 - 03:36 AM

haha my mistake. i don't see it earlier. it worked now. i used your way i have to make a new variable for assigning index. :) thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1