I went a help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • join

    #1

    I went a help

    I am a new student for language C
    , I do not have any course related to

    C and this question no related to any kind of home work or assignment

    I am truing to learning a C language and i went to execute some
    problem here and I went the code of this program . If you can help
    me do it please other wise think you for every body .
    ------------------------------------------------------------------------
    Write a program which can read a list of names, ids and gpas from a
    text file and store it into an array of structures. The text file
    might look as follows:

    Faisal 991234 3.5
    Aref 212334 4.0
    Fifi 913542 2.7
    Sarim 678235 1.3
    Majed 124523 4.0
    ……………………………………… ……………………………

    After the entire file is read, calculate the and print
    (i)the maximum of all gpas and the corresponding student name and ID
    (ii)the minimum of all gpas and the corresponding student name and ID
    (iii)the average of all gpas.

    I have to know the solution of this program then I will truing to
    solve some kind of these Questions?

  • Vladimir S. Oka

    #2
    Re: I went a help

    On Wednesday 15 March 2006 19:06, join opined (in
    <qKudna9Jbrkm-IXZnZ2dnUVZ_sad nZ2d@giganews.c om>):
    [color=blue]
    > I am a new student for language C, I do not have any course related to
    > C and this question no related to any kind of home work or assignment[/color]

    If you really want to learn C, read a good textbook (try K&R2 for a
    start), and try to solve this problem by yourself. Once you come across
    a specific problem, come back here and ask.
    [color=blue]
    > I am truing to learning a C language and i went to execute some
    > problem here and I went the code of this program . If you can help
    > me do it please other wise think you for every body .[/color]

    What follows is so obviously a homework assignment... Even if you're
    using somebody else's just in order to do some self-learning, you'd
    still be better off following my advice above.
    [color=blue]
    > -------------------------------------------------------------------
    > Write a program which can read a list of names, ids and gpas from a
    > text file and store it into an array of structures. The text file
    > might look as follows:
    >
    > Faisal 991234 3.5
    > Aref 212334 4.0
    > Fifi 913542 2.7
    > Sarim 678235 1.3
    > Majed 124523 4.0
    > Â…Â…Â…Â…Â…Â…Â…Â …Â…Â…Â…Â…Â…Â…Â… Â…Â…Â…Â…Â…Â…Â…Â …Â…Â…Â…
    >
    > After the entire file is read, calculate the and print
    > (i)the maximum of all gpas and the corresponding student name and ID
    > (ii)the minimum of all gpas and the corresponding student name and ID
    > (iii)the average of all gpas.
    >
    > I have to know the solution of this program then I will truing to
    > solve some kind of these Questions?[/color]

    --
    BR, Vladimir

    "Mind if I smoke?"
    "I don't care if you burst into flames and die!"

    Comment

    • ankisharma@gmail.com

      #3
      Re: I went a help


      join wrote:[color=blue]
      > I am a new student for language C
      > , I do not have any course related to
      >
      > C and this question no related to any kind of home work or assignment
      >
      > I am truing to learning a C language and i went to execute some
      > problem here and I went the code of this program . If you can help
      > me do it please other wise think you for every body .[/color]
      if ur real intent is to learn C then why don't u give it a shot. Try to
      write a program and if u face any problem, comp.lang.c is here[color=blue]
      > ------------------------------------------------------------------------
      > Write a program which can read a list of names, ids and gpas from a
      > text file and store it into an array of structures. The text file
      > might look as follows:
      >
      > Faisal 991234 3.5
      > Aref 212334 4.0
      > Fifi 913542 2.7
      > Sarim 678235 1.3
      > Majed 124523 4.0
      > ............... ............... ............... ............... ............... ...
      >
      > After the entire file is read, calculate the and print
      > (i)the maximum of all gpas and the corresponding student name and ID
      > (ii)the minimum of all gpas and the corresponding student name and ID
      > (iii)the average of all gpas.
      >
      > I have to know the solution of this program then I will truing to
      > solve some kind of these Questions?[/color]

      Comment

      • Default User

        #4
        Re: I went a help

        ankisharma@gmai l.com wrote:

        [color=blue]
        > if ur real intent is to learn C then why don't u give it a shot. Try
        > to write a program and if u face any problem, comp.lang.c is here[/color]


        It facilitates communication on this newsgroup if you don't use
        shorthand like "u" and "ur".




        Brian

        Comment

        • Paul

          #5
          Re: I went a help

          Hi join,

          There are lots of sites you can get help learning C. I visit here quite
          often :-) Join up (its free) and post a question and you should get
          help. However, you will be expected to do the coding yourself so try
          the exercise and post what you've managed to write.

          Get answers to your C# questions from our top industry experts by using Experts Exchange's platform to find the best C# help & solutions. Click to learn more!


          Paul

          Comment

          • Keith Thompson

            #6
            Re: I went a help

            "Paul" <CaswellPaulA@A OL.com> writes:[color=blue]
            > Hi join,
            > There are lots of sites you can get help learning C. I visit here quite
            > often :-) Join up (its free) and post a question and you should get
            > help. However, you will be expected to do the coding yourself so try
            > the exercise and post what you've managed to write.
            >
            > http://www.experts-exchange.com/Prog...g_Languages/C/[/color]

            Please provide context; see <http://cfaj.freeshell. org/google/>.

            I haven't looked at the above-mentioned site, so I can't comment on it
            directly. A lot of people have posted links here to C programming
            sites; most of them aren't very good. Caveat emptor.

            I'd be (pleasantly) surprised if that site where as good as
            comp.lang.c in terms of participation by actual experts and correction
            of mistakes.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Richard Heathfield

              #7
              Re: I went a help

              Keith Thompson said:
              [color=blue][color=green]
              >> http://www.experts-exchange.com/Prog...g_Languages/C/[/color]
              >[/color]
              <snip>[color=blue]
              >
              > I'd be (pleasantly) surprised if that site where as good as
              > comp.lang.c in terms of participation by actual experts and correction
              > of mistakes.[/color]

              I don't think you're going to be pleasantly surprised.

              --
              Richard Heathfield
              "Usenet is a strange place" - dmr 29/7/1999

              email: rjh at above domain (but drop the www, obviously)

              Comment

              • join

                #8
                re:I went a help

                that what i did auntil now but i cause i problem , the output of this
                code id

                null 0.000 0


                what the problem here?

                also, what is the best way of finding the max,min ,average of the
                gpa?


                code:
                #include <stdio.h>

                #include <stdlib.h>
                typedef struct student {
                char *name;
                int id;
                float gpa;
                } stu;

                stu nig[5];
                int main(){
                int i,n,avg,min,max ;
                int id;
                float gpa;
                char name[5];
                FILE *inputFile;
                inputFile = fopen("q4.txt", "r");

                if (inputFile) printf("File succesfully opened for
                reading!\n");
                else {
                printf("File not opened\n");
                exit(1);
                }


                for(i=0;i<5; i++){
                fscanf(inputFil e, "%s %d
                %f",nig.name,ni g[i].id,nig[i].gpa);



                printf("are:%s% d,%f",nig[i].name,nig[i].id,nig[i].gpa);

                //}
                fclose(inputFil e);

                }
                return 0;

                }

                Comment

                • Nick Keighley

                  #9
                  Re: I went a help

                  always leave some context in your post. Ie. include what you are
                  replying to (like I did).

                  join wrote:[color=blue]
                  > that what i did auntil now but i cause i problem , the output of this
                  > code id
                  >
                  > null 0.000 0
                  >
                  > what the problem here?[/color]

                  see my comments furthur down
                  [color=blue]
                  > also, what is the best way of finding the max,min ,average of the
                  > gpa?[/color]

                  for min and max simply scan the array (use a for loop) for the
                  smallest and largest values so far. For an average add the numbers
                  up and divide by the number of numbers. If you're clever you'll be
                  able to use one loop to do all three tasks!
                  [color=blue]
                  > code:[/color]

                  <snip>

                  by the time I saw your code it looked like this:-
                  [color=blue]
                  > stu nig[5];
                  > int main(){
                  > int i,n,avg,min,max ;
                  > int id;
                  > float gpa;
                  > char name[5];
                  > FILE *inputFile;
                  > inputFile = fopen("q4.txt", "r");[/color]

                  <snip>

                  well-laid out code is much easier to read. Your code may have
                  been corrupted by your posting software (or my news reader).
                  Avoiding tabs in posted code may help.

                  This is your code relaid out:-
                  My comments are tagged like this /***

                  <code>

                  #include <stdio.h>
                  #include <stdlib.h>

                  typedef struct student
                  {
                  char *name;
                  int id;
                  float gpa;
                  } stu;

                  stu nig[5];


                  int main()
                  /***
                  int main (void)
                  is better */

                  {
                  int i, n, avg, min, max;
                  /*** try to avoid multiple declarations on one line */

                  int id;
                  float gpa;
                  /*** use double unless there is good reason */

                  char name[5];
                  /*** what if someone has a name with more than 4 characters in it? */

                  FILE *inputFile;

                  inputFile = fopen("q4.txt", "r");

                  if (inputFile)
                  /*** good! you checked the return value! */

                  printf("File succesfully opened for reading!\n");
                  else
                  {
                  printf("File not opened\n");
                  exit(1);
                  /*** technically exit(1) is implementation defined. Use EXIT_FAILURE */

                  }

                  for (i = 0; i < 5; i++)
                  /*** try to avoid embedding constants in your code. Use a macro like
                  #define NUMBER_OF_RECOR DS 5
                  */

                  {
                  fscanf(inputFil e, "%s %d %f", nig.name, nig[i].id, nig[i].gpa);
                  /*** eek!
                  1. ***always*** check the return value of fscanf() etc.
                  2. name doesn't point at anything. You just wrote the name to
                  the null pointer. This is Very Bad
                  3. fscanf(0 wants *pointers* to integers and floats. You've written
                  to address zero. Very Bad. (note these values are only zero
                  because nig is at file scope).
                  */

                  printf("are:%s% d,%f", nig[i].name, nig[i].id, nig[i].gpa);
                  //}
                  /** what's this?

                  fclose(inputFil e);
                  }

                  return 0;
                  /*** good! main() returned something */
                  }

                  </code>


                  --
                  Nick Keighley

                  Comment

                  • join

                    #10
                    re:I went a help

                    char name[5];
                    /*** what if someone has a name with more than 4 characters in it? */


                    5 name for the number of names in input file that what I did.

                    printf("are:%s% d,%f", nig.name, nig[i].id, nig[i].gpa);
                    //}
                    /** what's this?

                    I went to read the names and ids and gpas than I went to print them

                    if it is wrong what is the correction?

                    Comment

                    • Nick Keighley

                      #11
                      Re: I went a help

                      LEAVE SOME CONTEXT IN YOUR POST!!!!!

                      I told you this last time. I posted an example with context. Please do
                      the same. If you are using google then follow the advice given in other

                      posts on how to work around it. Note *I* post from google and I can
                      make it work.


                      join wrote:[color=blue]
                      > char name[5];
                      > /*** what if someone has a name with more than 4 characters in it? */
                      >
                      > 5 name for the number of names in input file that what I did.[/color]

                      I'm having troble understanding you. There are names in the original
                      data file that are five characters long. These names will require 6
                      characters of storage (one for the terminating '\0' (nul character)).
                      6 characters won't fit in array of 5 chars.

                      The fact that your data file contained five records is irrelevent.
                      [color=blue]
                      > printf("are:%s% d,%f", nig.name, nig[i].id, nig[i].gpa);
                      > //}
                      > /** what's this?[/color]

                      I meant the "//}". I assume it's just sloppiness

                      [color=blue]
                      > I went to read the names and ids and gpas than I went to print them
                      >
                      > if it is wrong what is the correction?[/color]

                      see my previous post, where I pointed out several errors in your code.


                      --
                      Nick Keighley

                      Comment

                      • Default User

                        #12
                        Re: I went a help

                        Nick Keighley wrote:
                        [color=blue]
                        > LEAVE SOME CONTEXT IN YOUR POST!!!!!
                        >
                        > I told you this last time. I posted an example with context. Please do
                        > the same. If you are using google then follow the advice given in
                        > other posts on how to work around it.[/color]

                        That doesn't seem to be his problem, his headers indicate:

                        User-Agent: newsSync (C++Talk.NET)


                        I'm guessing that's one of those other usenet->web forum sites, but I'm
                        not sure. It's possible that it has no auto-quote functionality, so he
                        needs to do it himself in that case.




                        Brian

                        Comment

                        • Keith Thompson

                          #13
                          Re: I went a help

                          "Default User" <defaultuserbr@ yahoo.com> writes:[color=blue]
                          > Nick Keighley wrote:
                          >[color=green]
                          >> LEAVE SOME CONTEXT IN YOUR POST!!!!!
                          >>
                          >> I told you this last time. I posted an example with context. Please do
                          >> the same. If you are using google then follow the advice given in
                          >> other posts on how to work around it.[/color]
                          >
                          > That doesn't seem to be his problem, his headers indicate:
                          >
                          > User-Agent: newsSync (C++Talk.NET)
                          >
                          >
                          > I'm guessing that's one of those other usenet->web forum sites, but I'm
                          > not sure. It's possible that it has no auto-quote functionality, so he
                          > needs to do it himself in that case.[/color]

                          Then maybe (and I can't believe I'm saying this) he should switch
                          to Google Groups.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                          We must do something. This is something. Therefore, we must do this.

                          Comment

                          • Default User

                            #14
                            Re: I went a help

                            Keith Thompson wrote:
                            [color=blue]
                            > "Default User" <defaultuserbr@ yahoo.com> writes:[color=green]
                            > > Nick Keighley wrote:
                            > >[color=darkred]
                            > >> LEAVE SOME CONTEXT IN YOUR POST!!!!!
                            > >>
                            > >> I told you this last time. I posted an example with context.[/color][/color]
                            > Please do >> the same. If you are using google then follow the advice
                            > given in >> other posts on how to work around it.[color=green]
                            > >
                            > > That doesn't seem to be his problem, his headers indicate:
                            > >
                            > > User-Agent: newsSync (C++Talk.NET)
                            > >
                            > >
                            > > I'm guessing that's one of those other usenet->web forum sites, but
                            > > I'm not sure. It's possible that it has no auto-quote
                            > > functionality, so he needs to do it himself in that case.[/color][/color]

                            I took a bit of a search, and I was right. It's a usenet to PHPBB forum:


                            [color=blue]
                            > Then maybe (and I can't believe I'm saying this) he should switch
                            > to Google Groups.[/color]

                            I notice that the messages there have a button that says, "reply with
                            quotes", but without registering I can't try it to see how that works.
                            Most of those forums have their own quoting method using a tag system
                            that I doubt would translate well to newsgroups.



                            Brian

                            Comment

                            • Barry Schwarz

                              #15
                              Re: I went a help

                              On Fri, 17 Mar 2006 05:06:17 -0600,
                              h_u_s2002@hotma il-dot-com.no-spam.invalid (join) wrote:
                              [color=blue]
                              >that what i did auntil now but i cause i problem , the output of this
                              >code id
                              >
                              > null 0.000 0[/color]

                              How did you execute the code when it has syntax errors? Is this your
                              real code or did you retype it? You should always use cut and paste.
                              [color=blue]
                              >
                              >
                              >what the problem here?
                              >
                              >also, what is the best way of finding the max,min ,average of the
                              >gpa?
                              >
                              >
                              >code:
                              > #include <stdio.h>
                              >
                              >#include <stdlib.h>
                              >typedef struct student {
                              > char *name;
                              > int id;
                              > float gpa;
                              >} stu;
                              >
                              > stu nig[5];
                              >int main(){
                              > int i,n,avg,min,max ;
                              > int id;
                              > float gpa;
                              > char name[5];
                              > FILE *inputFile;
                              > inputFile = fopen("q4.txt", "r");
                              >
                              > if (inputFile) printf("File succesfully opened for
                              >reading!\n") ;
                              >else {
                              > printf("File not opened\n");
                              > exit(1);
                              >}
                              >
                              >
                              > for(i=0;i<5; i++){
                              > fscanf(inputFil e, "%s %d
                              >%f",nig.name,n ig[i].id,nig[i].gpa);[/color]

                              nig.name is syntactically incorrect. You need nig[i].name, just like
                              with id and gpa. nig is an array of struct. nig[i] is the i-th
                              struct in the array. And nig[i].name is the pointer to char in that
                              struct.

                              Unfortunately, you have an additional problem. nig is a global array.
                              It is initialized by default such that each element of the array (in
                              this case that means each member of each element) is set to the
                              correct form of zero. Since name is a pointer, name is initialized to
                              NULL. Your call to fscanf directs that function to store the input
                              name there. You are not allowed to store data where a NULL pointer
                              points. You need to initialize all five nig[i].name pointers to point
                              somewhere in memory that belongs to your program.
                              [color=blue]
                              >
                              >
                              >
                              > printf("are:%s% d,%f",nig[i].name,nig[i].id,nig[i].gpa);[/color]

                              You should add a \n to the end of your format string.
                              [color=blue]
                              >
                              > //}
                              >fclose(inputFi le);
                              >
                              > }
                              > return 0;
                              >
                              > }[/color]


                              Remove del for email

                              Comment

                              Working...