While loops and if statements as well as pointer help!

• (2 Pages)
• 1
• 2

26 Replies - 436 Views - Last Post: 07 July 2012 - 07:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=284975&amp;s=1c0be521ff9761285c133241dfdfab61&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#16 Skydiver

• Code herder

Reputation: 1928
• Posts: 5,736
• Joined: 05-May 12

Re: While loops and if statements as well as pointer help!

Posted 06 July 2012 - 05:14 PM

```if(value > max)
max = value;
else if(value < min)
min = value;

```

Consider this: what happens when max = 10, min = 100, and value is 21? Why isn't min picking up 21?

Next consider this: what happens when max = 50, min = 50 and value is 10? Why is min picking up 10 in this case?

This post has been edited by Skydiver: 06 July 2012 - 05:15 PM

#17 jordannn15

Reputation: 0
• Posts: 25
• Joined: 06-July 12

Re: While loops and if statements as well as pointer help!

Posted 06 July 2012 - 06:07 PM

For the first one because min is greater than it and I'm assigning value to min as if it's less than. Is that correct?

For the second, I would think the same as the first but then I would be wrong. What am I not thinking about here?

#18 vividexstance

• D.I.C Lover

Reputation: 390
• Posts: 1,348
• Joined: 31-December 10

Re: While loops and if statements as well as pointer help!

Posted 06 July 2012 - 06:19 PM

The problem is that your min variable is initialized with a garbage value(probably a very high integer), and you use it for a comparison before you assign a value to the min variable. So just assign something to min, probably zero for starters and see if your code works then.

#19 Skydiver

• Code herder

Reputation: 1928
• Posts: 5,736
• Joined: 05-May 12

Re: While loops and if statements as well as pointer help!

Posted 06 July 2012 - 06:21 PM

jordannn15, on 06 July 2012 - 06:07 PM, said:

For the first one because min is greater than it and I'm assigning value to min as if it's less than. Is that correct?

No. You are looking at code and "wishing" it would do what you are thinking instead of just letting the code tell you what it is doing.

```if(value > max)
max = value;
else if(value < min)
min = value;

```

The compiler is actually seeing this
```if(value > max)
{
max = value;
}
else
{
if(value < min)
{
min = value;
}
}

```

So now step through the code with max = 10, min = 100, and value = 21.

What lines are executed?

Now try that again with max = 50, min = 50 and value = 10. What lines are executed?

What changes do you need to make to the code so that min and max are set correctly?

#20 jordannn15

Reputation: 0
• Posts: 25
• Joined: 06-July 12

Re: While loops and if statements as well as pointer help!

Posted 06 July 2012 - 06:34 PM

Okay so for the first question
```max = value;

min = value;

```

are executed but for question two
```min = value;

```

only that is executed correct?

#21 Skydiver

• Code herder

Reputation: 1928
• Posts: 5,736
• Joined: 05-May 12

Re: While loops and if statements as well as pointer help!

Posted 06 July 2012 - 07:08 PM

jordannn15, on 06 July 2012 - 06:34 PM, said:

Okay so for the first question
```max = value;

min = value;

```

are executed but for question two
```min = value;

```

only that is executed correct?

No.

Given the code:
For the first question:
```if(value > max)     // Executed: Is 21 > 10? Yes. Goto line 3
{
max = value;    // Executed: max = 21. Goto line 12
}
else
{
if(value < min)
{
min = value;
}
}

```

For the second question:
```if(value > max)     // Executed: Is 10 > 50? No. Goto line 7
{
max = value;
}
else
{
if(value < min)     // Executed: Is 10 < 50? Yes. Goto line 9
{
min = value;     // Executed: min = 10. Goto line 12
}
}

```

#22 jimblumberg

Reputation: 3055
• Posts: 9,291
• Joined: 25-December 09

Re: While loops and if statements as well as pointer help!

Posted 07 July 2012 - 05:44 AM

Get rid of the else. You need to compare the value to both min and max.

Jim

#23 jordannn15

Reputation: 0
• Posts: 25
• Joined: 06-July 12

Re: While loops and if statements as well as pointer help!

Posted 07 July 2012 - 05:27 PM

This should work right?
```#include <stdio.h>

int main(void)
{
FILE *inp; /* input file pointer */
FILE *out; /* output file pointer */

int count = 0;     /* number of values in file */
int sum = 0;      /* sum of the values in file */
int input_status; /* value returned by fscanf */
int value; /* first number in file */
int min;  /* minimum number */
int max; /* maximum number */

inp = fopen("data.dat", "r");
out = fopen("data.out", "w"); /* opening the input and output files */

input_status = fscanf(inp, "%d", &value); /* priming read - initialize */

while(input_status == 1){ /* reading until input_status doesn't equal 1 */

count++; /* increment count by 1 */

if(value > max)
max = value;
if(value < min)
min = value;

sum += value; /* increment sum */

input_status = fscanf(inp, "%d", &value); /* update */
}

/*print min, max, and average to output file */

fprintf(out, "Minimum value = %d\n", min);
fprintf(out, "Maximum value = %d\n", max);
fprintf(out, "Average value = %.4lf\n", (double) sum / count);

fclose(inp);
fclose(out); /* close input and output */

return 0;
}

```

#24 jimblumberg

Reputation: 3055
• Posts: 9,291
• Joined: 25-December 09

Re: While loops and if statements as well as pointer help!

Posted 07 July 2012 - 06:04 PM

Close but you need to initialize min and max before your loop. I suggest that after your "priming" read you assign these variables the value of the first entry.

Remember you must always initialize your variables before you use them in a calculation, or comparison.

Jim

#25 jordannn15

Reputation: 0
• Posts: 25
• Joined: 06-July 12

Re: While loops and if statements as well as pointer help!

Posted 07 July 2012 - 06:20 PM

but if I assign them like this:
```int min = value;
int max = value;

```
after the priming read then it gives me an error of re-declaration of min and max?
That's what you meant right assign min and max to value?

#26 Skydiver

• Code herder

Reputation: 1928
• Posts: 5,736
• Joined: 05-May 12

Re: While loops and if statements as well as pointer help!

Posted 07 July 2012 - 06:50 PM

This is a declaration:
```char ch;

```

This is an initialization:
```char ch = 'J';

```

This is an assignment:
```ch = 'J';

```

You can get rid of the declaration on lines 12-13 and just have the initialization after the priming.

This post has been edited by Skydiver: 07 July 2012 - 06:51 PM

#27 jordannn15

Reputation: 0
• Posts: 25
• Joined: 06-July 12

Re: While loops and if statements as well as pointer help!

Posted 07 July 2012 - 07:05 PM

Ohhhh thank you soooo much that worked!