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 PM Rate Topic: -----

#16 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1928
  • View blog
  • 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

With your code:
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

Was This Post Helpful? 0
  • +
  • -

#17 jordannn15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#18 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1928
  • View blog
  • 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

View Postjordannn15, 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.

Your original code is this:
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?
Was This Post Helpful? 0
  • +
  • -

#20 jordannn15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1928
  • View blog
  • 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

View Postjordannn15, 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
    }
}



Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is online

  • member icon

Reputation: 3055
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#23 jordannn15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#24 jimblumberg  Icon User is online

  • member icon

Reputation: 3055
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#25 jordannn15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#26 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1928
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#27 jordannn15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2