1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

New to c, i need help!!

Discussion in 'C' started by Chief, Oct 9, 2008.

  1. Chief

    Chief New Member

    Joined:
    Oct 9, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I m trying to do a programm with c and i have some questions about that.
    -How to find the min and the max from seven (int) values, this values is for each day of the week?
    -and the average of this values.

    THNX!!

    do {
    printf("Day_1:%d\n");
    scanf("%d",&val7);
    } while (val7>=0 || val7<17);
    return 0;

    do {
    printf("Day_2:%d\n");
    scanf("%d",&val7);
    } while (val7>=0 || val7<17);
    return 0;
    .
    .
    .
    .
    .
    float average;
    average=(val1+val2+val3+val4+val5+val6+val7)/7;
    scanf("%f",&average);
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    For Finding max all you have to be doing is loop through each to see which is bigger then the one stored in your temp and at the end of the lopp you will have max / min

    For average I guess you are doing it correctly
     
  3. Chief

    Chief New Member

    Joined:
    Oct 9, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    ok I change the code but, the average it doesn’t work correctly what I do wrong?:confused:
    Code:
    #include <stdio.h>
    
    main(){
    
    int val1;
    int val2;
    int val3;
    int val4;
    int val5;
    int val6;
    int val7;
    
    int min;
    int max;
    int sum;
    float average;
    
    
    printf("Give values of sunshine duration for the seven days of a week,\n starting from the value for Monday.\n Each value should be an integer in the range (0..16]:%d\n");
    
    do
    {
    	printf("Give for Monday: ");
    	scanf("%d",&val1);
    }while( ! (val1 >=0 && val1 <=16) );
    
    min = val1;
    max = val1;
    sum = val1;
    
    do
    {
    	printf("Give for Tuesday: ");
    	scanf("%d",&val2);
    }while( ! (val2 >=0 && val2 <=16) );
    
    if(min<val2)
    {
    	min= val2;
    }
    
    if(max>val2)
    {
    	max= val2;
    }
    
    sum += val2;
    
    do
    {
    	printf("Give for Wednesday: ");
    	scanf("%d",&val3);
    }while( ! (val3 >=0 && val3 <=16) );
    
    if(min<val3)
    {
    	min= val3;
    }
    
    if(max>val3)
    {
    	max= val3;
    }
    
    sum += val3;
    
    do
    {
    	printf("Give for Thursday: ");
    	scanf("%d",&val4);
    }while( ! (val4 >=0 && val4 <=16) );
    
    if(min<val4)
    {
    	min= val4;
    }
    
    if(max>val4)
    {
    	max= val4;
    }
    
    sum += val4;
    do
    {
    	printf("Give for Friday: ");
    	scanf("%d",&val5);
    }while( ! (val5 >=0 && val5 <=16) );
    
    if(min<val5)
    {
    	min= val5;
    }
    
    if(max>val5)
    {
    	max= val5;
    }
    
    sum += val5;
    do
    {
    	printf("Give for Saturday: ");
    	scanf("%d",&val6);
    }while( ! (val6 >=0 && val6 <=16) );
    
    if(min<val6)
    {
    	min= val6;
    }
    
    if(max>val6)
    {
    	max= val6;
    }
    
    sum += val6;
    do
    {
    	printf("Give for Sunday: ");
    	scanf("%d",&val7);
    }while( ! (val7 >=0 && val7 <=16) );
    
    if(min<val7)
    {
    	min= val7;
    }
    
    if(max>val7)
    {
    	max= val7;
    }
    
    sum += val7;
    
    average = sum/7;
    printf("Results:\n");
    printf("Max: %d, Min: %d Average: %f\n", min, max, average);
    printf("Number of days with the shortest sunshine =");
    
    
    
    return 0;
    }
     
  4. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    You need to learn the technique of dry running. Work through the code doing (on paper if necessary, eventually you'll be able to do it in your head) what the computer would be doing, making sure you are doing **_EXACTLY_** what the code says and NOT what you think the code says. Then think about it.

    For example, let's say min contains 5 and val2 contains 7, and the code is:
    Code:
    if(min<val2)
    {
    	min= val2;
    }
    
    min(5) is less than val2(7) so min<val2 evaluates TRUE.
    We now assign val2(7) to min, which increases its value from 5 to 7.

    Are you sure that's what's meant to happen?
    If not, how should the code be changed so that what you want to happen does so?
     
  5. Chief

    Chief New Member

    Joined:
    Oct 9, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Do you mean something like that? or I need more reading?.....:confused::D


    if(min<val2); {
    min=val2;}
    else if (min=val1);
     
  6. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    No, the syntax is completely wrong for start. Don't put a semicolon after the if expression. You need a code block after "else if".

    Can you express in English what you want it to do? Describe, not trying to use C, what should happen. Assume min contains 5 and val2 contains 7. If you can't do this, then you have no chance of writing correct software.

    For example (fill in the blanks):
    I want the computer to compare min and val2.
    If ___ is smaller, then I want it to assign ___ to ___ so that min contains the value ___.

    Get the ideas clear in your own mind first. Is the following description of a similar manual process correct, and if not, why?

    On Monday I recorded a temperation of 5 degrees. I wrote that down as the minimum so far. On Tuesday I recorded 7 degrees. That's colder than 5 degrees, so I crossed out 5 and wrote down 7.
     
  7. Chief

    Chief New Member

    Joined:
    Oct 9, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    This is that you mean?
    min=val2;
    If (val2<min)
    {
    Val2=min;
    }
     
  8. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    You tell me. Here it is in English:

    On Tuesday I crossed out Monday's reading from the "minimum temperature so far" box and wrote down Tuesday's reading, without first checking which was smaller.
    Then I checked if Tuesday's reading was less than the current minimum and, if it was, replaced Tuesday's reading with the minimum temperature so far.

    Did you try filling in the blanks in my previous post?

    Did you read the description of the manual process; did it seem correct to you, and if not, why? (The description corresponds to the code you originally posted, by the way, just in case you can't see why it is relevant. And here's another clue, no, it isn't correct. But I want YOU to tell ME why it isn't.)

    Getting the ideas straight in your head is really the best way to write code. Don't try writing code before you know what you're doing. What thought processes led to the code you just put in, or did you just shuffle stuff around in the hope that it would work this time? (Another hint while I'm feeling generous: that method of writing code NEVER WORKS. I know; I've tried it; the only solution is to sit down with a blank piece of paper and work out what goes where when.)

    Suppose min contains Monday's reading (val1), which was 5, and Tuesday's reading (val2) was 7. What will "min=val2;" do? Supposing the "if" were to evaluate true; why would you want to assign anything to val2 anyway? Should it change after it has been set to Tuesday's reading?
     
  9. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    If you are using random shuffling to write code, work out what has to happen regardless. Surely the first thing to do is compare values (min and val2), do you agree? So the first statement must be "if (___ < ___)". Are we checking if min is less than val2, or if val2 is less than min? If you're not sure, give them some example values.

    In the if block, having decided which of min and val2 is smaller, we want to assign something to min, because the whole point of this is to adjust min if necessary. So the statement in the braces must be something like "min = ___;".

    So this gives us the following code, which MUST be in the right place regardless of what other things we are going to shuffle round:
    Code:
    if (___ < ___)
    {
      min = ___;
    }
    
    Now fill in the blanks.
     
  10. Chief

    Chief New Member

    Joined:
    Oct 9, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Maybe the problem is that, I think like a human not like a pc, if I ask you, to day is colder than yesterday or yesterday was wormer than today, the mining is the same…. That is “I thing†the different, but not for a pc and sometimes this confusing me.
    Anyway of course in first case I said that the 7 is smaller than 5 and I changed min from 5 to7.

    If (val2<min)
    {
    (min=val2);
    }
     
  11. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Almost, but you don't need brackets around the assignment.

    Thinking computer logic is different could be where you're going wrong; logic is logic whether human, mathematical, computer or whatever. Today colder than yesterday/yesterday warmer than today are of course identical, as are the following expressions (in both maths and software):
    Code:
    temp_today < temp_yesterday
    temp_yesterday > temp_today
    
    Just as in English, where "yesterday colder than today" and "yesterday warmer than today" are opposites, so also are:
    Code:
    temp_yesterday < temp_today
    temp_yesterday > temp_today
    
    An essential skill for programmers is to learn how to think like a computer. Look at the code and do EXACTLY what it says, not what you think it says, not what you want it to say etc. To find bugs you must master this skill and eventually (as I can) you will be able to do it in your head. Complex bugs will still need serious analysis. To master this skill you will need to start by working through code bit by bit on paper. Assume certain inputs and see what the code does when you dry run it.

    When the program doesn't do what you expect it to do, a useful hint is to start displaying (with extra printf statements) values of variables, where you are in the code, and you can be as verbose as you need to be. For example you could augment some of the code as follows:
    Code:
    printf("Determining the minimum so far; testing if min(%d)<val2(%d) (%s)\n",
      min,val2, (min<val2)?"TRUE":"FALSE");
    if(min<val2)
    {
    	printf("Assigning val2(%d) to min, overwriting %d\n",val2,min);
    	min= val2;
    }
    
    So if min contains 5 and val2 contains 7 this would display:
    Code:
    Determining the minimum so far; testing if min(5)<val2(7) (TRUE)
    Assigning val2(7) to min, overwriting 5
    
    This should make the bug obvious, and also hopefully the solution to the bug (reverse the "less-than" operator).
     

Share This Page