Disk IP/OP

This is a discussion on Disk IP/OP within the C Programming forums, part of the General Programming Boards category; Help........! I think writing a program that read and write datas from a file and onto another file is quite ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    16

    Disk IP/OP

    Help........! I think writing a program that read and write datas from a file and onto another file is quite complicated. I dun know what to do. Can anyone please explain the following:

    write a c program to determine the shortest, longest side of a triangle and its perimeter and area . Its sides are 4,6,10 which is stored onto a file call sides.txt. Then print their values to a new file.

    What should I do?

    Help...!

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You should try first then post the code when you have trouble and then be happy if someone helps with just a small part of it.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    I did try. However, I manage to get the program done a moment ago. But I dun know how to find the area. Can any one help? Also, is my step too long? Is there any shortest methods? This question is killing me for a few days .

    I tried to post but window prompted: Pls use code tags to post code containing and . How to post? What code tags??

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    I did try. However, I manage to get the program done a moment ago. But I dun know how to find the area. Can any one help? Also, is my step too long? Is there any shortest methods? This question is killing me for a few days .

    Hey I manage to post it in . Dun know why initially cannot. Anyway , pls advise.



    #include <stdio.h>
    #include <math.h>

    int main(void)
    {
    int i, a[3];
    FILE *input;
    void shortest (int []);
    void longest (int []);
    void peri_area (int []);

    input = fopen("sides.txt", "r");
    if (input==NULL)
    {
    printf("Error!\n");
    return 0;
    }

    for (i=0;i<3;i++)
    fscanf(input, "%d", &a[i]);

    shortest (a);
    longest (a);
    peri_area (a);

    fclose(input);
    return 0;
    }

    void shortest (int a[])
    {
    FILE *output=fopen("shortest.txt", "w");
    int i, shortest=a[0];
    for (i=0;i<3;i++){
    if ( a[i]<shortest)
    shortest = a[i];
    }
    fprintf(output, "%s%d", "\nShortest side is ", shortest);
    fclose(output);
    }

    void longest (int a[])
    {
    FILE *output=fopen("longest.txt", "w");
    int i, longest=a[0];
    for (i=0;i<3;i++){
    if ( a[i]>longest)
    longest = a[i];
    }
    fprintf(output, "%s%d", "\nLongest side is ", longest);
    fclose(output);
    }
    Last edited by jnwk888hwq; 10-17-2003 at 11:01 AM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >How to post? What code tags??

    These.

    [EDIT]Like this:
    [code]/* your code here */[/code]
    Last edited by Dave_Sinkula; 10-17-2003 at 09:20 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    I'm sure you can google for the method on finding area in a triangle... just write in code the formula.
    Last edited by w00tsoft; 10-17-2003 at 11:10 AM.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    Originally posted by w00tsoft
    I'm sure you can google for the method on finding area in a triangle... just write in code the formula.
    I really dun know. I know 1/2 x base x height but what about this?

    Can someone please help?

    Also, aint the program too long. I know shorter program do exist. How to shorten this program?

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    You should put the same effort into your programming as in asking questions...You'd be finished by now...

    return((baseline*height)/2);

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    Anyway, my friends managed to get the program BUT still got one problems - area always becomes zero. How to overcome it?


    #include <stdio.h>
    #include <math.h>

    int main(void)
    {
    void values(int []);

    FILE *sides;
    int i, length[3];
    sides = fopen("sides.txt", "r");
    if (sides == NULL)
    {
    printf("Error!\n");
    return 0;
    }

    for (i=0; i<3; i++)
    fscanf(sides, "%d", &length[i]);

    values(length);

    fclose(sides);
    return 0;
    }

    void values(int length [])
    {
    FILE *output=fopen("output.txt","w");
    int i, shortest=length[0], longest=length[0], perimeter=0;
    double area, s;

    fprintf(output, "Values of triangle:\n");
    //Calculate shortest & longest sides
    for (i=0;i<3;i++)
    {
    if ( length[i]<shortest)
    shortest = length[i];
    if ( length[i]>longest)
    longest = length[i];
    }
    fprintf(output, "%s%d", "\nShortest side is ", shortest);
    fprintf(output, "%s%d", "\nLongest side is ", longest);


    //Perimeter
    for (i=0; i<3; i++)
    perimeter += length[i];

    fprintf(output, "\nPerimeter is %d\n", perimeter);



    //Area
    s = perimeter/2.0;
    area = s*(s-length[0])*(s-length[1])*(s-length[2]);
    area = pow(area, 0.5);

    fprintf(output, "%s%.2lf", "\nArea is ", area);

    fclose(output);

    }

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Your area calculation is wrong.

    using 10, 4, 6 as side lengths, perimeter is 20.

    s = perimeter/2.0; make s= 10
    area = s * (s-length[0]) * (s-length[1]) * (s-length[2]);
    which makes (s-length[0]) = 0.

    Also, you can make each of your fprintf() statements easier byt not outputting a constant string. Change them to

    fprintf(output, "Area is %.2f \n", area);

    And you didn't use code tags -- click on the link Dave_Sinkula gave you to answer how to use them.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    Originally posted by WaltP
    Your area calculation is wrong.

    using 10, 4, 6 as side lengths, perimeter is 20.

    s = perimeter/2.0; make s= 10
    area = s * (s-length[0]) * (s-length[1]) * (s-length[2]);
    which makes (s-length[0]) = 0.

    Also, you can make each of your fprintf() statements easier byt not outputting a constant string. Change them to

    fprintf(output, "Area is %.2f \n", area);

    And you didn't use code tags -- click on the link Dave_Sinkula gave you to answer how to use them.
    Then how should I calculate area? I can't think of any formula. Please help.

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    No. Your area calculation is correct.
    If the 3 lengths are 10, 6 and 4 then the area is 0.
    Thats perfectly correct.
    Just try to draw that triangle and you will see why.

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    Correct me if I'm wrong, but it would seem that the 3 sides of 4, 6, 10 seems highly unlikely? I can't draw a triangle with those measurements.

    Pythagoras ain't gonna be happy if he sees his formula not working today....

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    oh and by the way, code tags are basically using "
    Code:
    " and "
    " tags at the start and end of your codes respectively to denote the indents and start/end of your program. hope it helps...

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    sorry.....i messed it up.

    (code) and (/code) but using square brackets instead.

    apologies for a confusing reply...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disk failure in 3 disk RAID0 & 5?
    By cpjust in forum Tech Board
    Replies: 12
    Last Post: 12-22-2008, 09:09 AM
  2. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 05:18 PM
  3. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM
  4. Towers of Hanoi, special output.
    By spoon_ in forum C Programming
    Replies: 3
    Last Post: 03-15-2003, 05:08 PM
  5. Strange hard disk
    By GanglyLamb in forum Tech Board
    Replies: 20
    Last Post: 03-01-2003, 03:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21