User CP :: Log in to check your private messages :: Chat :: Register :: Log in

Board Index :: Album :: FAQ :: Calendar :: Members :: Groups :: Staff ::
Search
Post new topic   Reply to topic
graphic C programing final graphic
Author Message PO Info
Buda
Maille smith extrodanair



Gender: Gender:Male
Joined: 30 Sep 2004
PostPosted: Thu May 03, 2007 7:57 pm    Post subject: C programing final

Ok, so i got out of my Introduction to engineering computations (preaty much just a intro to the C language) final a coupla hours ago, and let me tell you it was easy, up till the last question.....
it was a freaking doozey

Maby not for experianced programers, but for an intro class it was very difficult.

the premis was this, you had to write a program that read a txt file with three columns of numbers, an ID a quantity, and a unit price. (as in a stores inventory)
a samle would be
101 200 3.0
131 34 30.0
104 20 15.0
102 30 16.0
and so on, (several IDs were repeated)

The program had to write a text file that first told the number of unique products, added the quantaties of the products, and then sorted them in ascending order, and then calculated the total cost of the entire inventory.
The program also had to be flexible enough to acomodate any number of lines and duplications.(he let us slide on the any number of lines part, and we were able to define a vaiable "N" that could be edited in the top of the program with the number of lines in the text file) though i relise now that i probaly could have just done a while(file!=EOF) loop, and had it do N++ at the end of the loop to count the number of lines and work with that.
The professer had to extend the exam for an hour so that most of the class(that hadnt given up) could finish

Being one of the few that finished with a (compleatly)working program, I feel proud of my self and wanted to share.

and in case your intrested, this is my program.

Code:

#include <stdio.h>
#include <stdlib.h>
/* number of lines of code */
#define N 7
/* this is so i dont have to edit lots of lines to switch between terminal output and text file output when debugging */
#define OUT outputfile

  struct inventory
  {
     int id, quant;
     float price;
  };
  struct inventory c[N];
  int unIDs[N]={0};
 
 
/* This function checks for the number of unique ids */
int uniqueprod(void)
{
    int i, j, k=0, un;
    for(i=0; i<N; i++)
      {
      un=1;
         for(j=0; j<N; j++)
              {
              if(c[i].id == unIDs[j])
                      {
                      un=0;
                      }
              }
      if(un==1)
        {
        unIDs[i]=c[i].id;
        k++;
        }
      } 
     return k;
}   

/*this function calculates price */
float calccost(float quant, float price)
{
      float total;   
      total=quant*price;
      return total;
}     

               
int main(int argc, char *argv[])
{
  int i, j, numID, tempID;
  float totalinvcost=0.0;
  FILE *inputfile;
  FILE *outputfile;
 
  outputfile=fopen("sortedinventory.txt", "w");
 
  if ((inputfile=fopen("inventory.txt", "r"))==NULL)
     {
     printf("***data file could not be opened please try again***");
     system("PAUSE");
     return 0;
     }
     
  for(i=0; i<N; i++)
     {
     fscanf(inputfile, "%d      %d  %f\n", &c[i].id, &c[i].quant, &c[i].price);
     }
  numID=uniqueprod();
  struct inventory unc[numID];
  j=0;
  /*put all unique IDs into an array */
  for(i=0; i<N; i++)
     {
     if(unIDs[i] != 0)
        {
        unc[j].id=unIDs[i];
        j++;
        }
     }

   
  /*sort those IDs */
  for(i=0; i<numID; i++)
      {
           for(j=i+1; j<numID; j++)
           {
                  if(unc[i].id>unc[j].id)
                    {
                    tempID=unc[i].id;
                    unc[i].id=unc[j].id;
                    unc[j].id=tempID;
                    }
           }                                                     
      }
     
     
  /* set all quantities for unique IDs to 0 */   
  for(i=0; i<numID; i++)
    {
    unc[i].quant=0;
    }

  /* if the IDs match, add the quantity of orginal to unique, and set unit price */
  for(i=0; i<numID; i++)
     {
      for(j=0; j<N; j++)
        {
        if(unc[i].id==c[j].id)
            {
            unc[i].quant=unc[i].quant+c[j].quant;
            unc[i].price=c[j].price;
            }
        }
     }
/*  calculate total inventory cost */
  for(i=0; i<numID; i++)
    {
      totalinvcost=totalinvcost+calccost(unc[i].quant, unc[i].price);
    }
 
  /*prints to file */
  fprintf(OUT, "There are %d kinds of product\n", numID);
  fprintf(OUT, "\n");
  fprintf(OUT, "Product ID         Total Quantity        Unit Price\n"); 
  for(i=0; i<numID; i++)
     {
     fprintf(OUT, "%d              %d                    %f\n", unc[i].id, unc[i].quant, unc[i].price);
     }
  fprintf(OUT, "Total inventory cost = %f\n", totalinvcost);
  fclose(inputfile);
  fclose(outputfile);
 
  system("PAUSE");   
  return 0;
}

_________________


Avatar art by Scrum Yummy (and its awsome)
Quote:
Recently I overheard two co-workers, the first of which was training the other one.
Co-Worker #1: "A boolean variable has two possible values: true or false."
Co-Worker #2: "Umm...true?"
Reply with quote
Wins 56 - Losses 66
Level 13
EXP: 2709
HP: 3135
Eligible for battle!
STR: 755
END: 1190
ACC: 1090
AGI: 665
Tharael, Brooding Evil (Sword)
(230 - 630)
Back to top
View user's profile Send private message MSN Messenger
Shino
Fade into this fantasy, caught in the web of time


Age: 49
Gender: Gender:Male
Joined: 15 Sep 2002
PostPosted: Fri May 04, 2007 8:48 am    Post subject:

Buda, first I edited your post and put your code into a [ code ] tag. It makes all the text fixed width and saves your formatting.

As for your program. Very well done! Considering this is a final, and you were being timed on it, I'm pretty impressed.

Maybe if I get time, I'll show you the Java version of this. Wink

_________________
So many games... so little time


Reply with quote
Wins 190 - Losses 169
Level 21
EXP: 11590
HP: 3150
Eligible for battle!
STR: 1050
END: 1050
ACC: 1200
AGI: 1200
Bianco & Nero (Sabers)
(500 - 600)
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Buda
Maille smith extrodanair



Gender: Gender:Male
Joined: 30 Sep 2004
PostPosted: Fri May 04, 2007 9:48 am    Post subject:

Shino wrote:
Buda, first I edited your post and put your code into a [ code ] tag. It makes all the text fixed width and saves your formatting.

thanks, I didnt think to do that.

Shino wrote:

As for your program. Very well done! Considering this is a final, and you were being timed on it, I'm pretty impressed.

Maybe if I get time, I'll show you the Java version of this. Wink

thanks for the compliment! I think I would be intrested in seeing this in java heh

_________________


Avatar art by Scrum Yummy (and its awsome)
Quote:
Recently I overheard two co-workers, the first of which was training the other one.
Co-Worker #1: "A boolean variable has two possible values: true or false."
Co-Worker #2: "Umm...true?"
Reply with quote
Wins 56 - Losses 66
Level 13
EXP: 2709
HP: 3135
Eligible for battle!
STR: 755
END: 1190
ACC: 1090
AGI: 665
Tharael, Brooding Evil (Sword)
(230 - 630)
Back to top
View user's profile Send private message MSN Messenger
Silver Adept
Otaku Lord


Age: 41
Gender: Gender:Male
Joined: 20 May 2003
PostPosted: Fri May 04, 2007 1:34 pm    Post subject:

Looks good, Buda! Even though I don't know C, my experience in Ruby made that code look okay. I can show you how it would be done in Ruby on Rails. It would probably have a few less lines of code, but produce the same effect. I didn't learn how to output to file or input from file, though, so I'd have to pick up that part.

_________________
Sir Silver Adept, KCI. Check out the Knights of Jubal if you want to revive chivalrous behavior.
Reply with quote
Wins 293 - Losses 240
Level 23
EXP: 2163
HP: 3375
Eligible for battle!
STR: 1125
END: 1125
ACC: 1225
AGI: 1225
Sander's Asylum (Partisan)
(505 - 655)
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
graphic graphic
Post new topic   Reply to topic Board Index -> Geek Haven
Page 1 of 1
All times are GMT - 4 Hours
Email this topic to a friend

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum