assignment help

This is a discussion on assignment help within the C Programming forums, part of the General Programming Boards category; I'm pretty new to C programming and have just started a C programming module on my uni course and I ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    assignment help

    I'm pretty new to C programming and have just started a C programming module on my uni course and I am looking for some guidance on an assignment I have recently been set.

    The specification is as follows:

    You are required to write a C program suitable for aiding the calculation of travel expenses by the finance department of a small company. Your application is to be produced in simple form (Part 1) and then modified (Part 2) once examination and approval by your tutor is given.
    Requirements for part 1
    Your application should allow the population of a chart containing the distances in miles (or kilometers) between points. The chart should cater for 10 separate points. On completion of the entry of the distances the application should display a menu from which options can be selected. These options are:
    1. The user should be able to display the distance between two valid points (e.g. point 1 and point 6) input by the user
    2. The user should be able to calculate the cost of a journey between two valid points e.g. point 1 and point 6 at a predefined rate of 40 pence per mile. The output should be displayed in a suitable format.
    3. The user should be able to display to the screen the complete mileage chart in a format similar to the one below( see fig 1).


    I am just looking for some general pointers in the right direction if possible as I am struggling starting it.

    Thanks for looking.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Make a starting attempt and work from there.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       return 0;
    }
    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.*

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Hmmm. Well, why don't you start out with the code you have and ask questions about code related?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    don't think this makes much sense but this is what i have so far:

    Code:
    #include <stdio.h>
    
    #define COST 40
    #define ROWS 10
    #define COLS 10
    
    int menu(void);
    void dispDistance(void);
    void journeyCost(void);
    void dispChart(void);
    int array[ROWS][COLS];
    
    int main(void)
    {
       int choice;
    
       while(choice !='q')
       {
         switch()
         {
          case '1':/*display distance*/
                dispDistance();
          break;
          case '2':/*calculate cost of journey*/
                journeyCost();
          break;
          case '3':/*display the chart*/
                dispChart();
          break;
          default:
              puts("Please enter a valid number");
         }/*end switch*/
    
        }/*end while*/
    
          return 0;
    }
    
    int menu(void)
    {
          int menuChoice;
          puts("Menu");
          Puts("1) Display distance between two places");
          Puts("2) Calculate the cost of a journey");
          Puts("3) Display the full mileage chart");
          Puts("q) Quit\n");
          scanf("%d",&menuChoice);
    
          return choice;
    
    }
    
    void dispDistance()
    {
          int x,y;
    
          printf("Enter starting point: ");
       scanf("%d",&x);
    
          printf("Enter end point: ");
          scanf("%d",&y);
    
       if(x>ROWS-1 || x<COLS+1 || y>ROWS-1 || y<COLS+1)
       {
      printf("The distance between point x and point y is: %d \n", array[x-1][y-1]);
      return array[x-1][y-1];
       }
          else
          {
       printf("Invalid entry");
    
          }/*end if*/
    
          return 0;
    
    }/*end display distance*/
    
    void journeyCost()
    {
        float cost;
     cost = dist * 0.4;
     printf("Cost of the journey is: %f\n", cost);
    
    }/*end journey cost*/
    
    void dispChart()
    {
        int x,y;
     for(x=0;x<ROWS;x++)
     {
           printf("\n");
    
         for(y=0;y<COLS;y++)
            {
         printf("\t%d", array[x][y]);
         }/*end for*/
        }/*end for*/
    
     
    
    }/*end dispay chart*/
    any help would be much appreciated.

    Thanks

  5. #5
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    I've taken a look at this. Honestly, I think if you tried to compile it you would find a lot of your problems.

    You use Puts() instead of puts() in several places.
    You have a prototype for int main().
    You never define or initialize dist in journeyCost().
    You return choice instead of menuChoice in menu().
    You don't have a value that you're switching on in main().

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by mb86 View Post
    don't think this makes much sense but this is what i have so far:

    Code:
    #include <stdio.h>
    
    #define COST 40
    #define ROWS 10
    #define COLS 10
    
    int menu(void);
    void dispDistance(void);
    void journeyCost(void);
    void dispChart(void);
    int array[ROWS][COLS]; //is this a global variable declaration?
    
    int main(void)
    {
       int choice;
    
       while(choice !='q')
       {
         switch()
         {
          case '1':/*display distance*/
                dispDistance();
          break;
          case '2':/*calculate cost of journey*/
                journeyCost();
          break;
          case '3':/*display the chart*/
                dispChart();
          break;
          default:
              puts("Please enter a valid number");
         }/*end switch*/
    
        }/*end while*/
    
          return 0;
    }
    
    int menu(void)
    {
          int menuChoice;
          puts("Menu");
          Puts("1) Display distance between two places");
          Puts("2) Calculate the cost of a journey");
          Puts("3) Display the full mileage chart");
          Puts("q) Quit\n");
          scanf("&#37;d",&menuChoice);
    
          return choice;
    
    }
    
    void dispDistance()
    {
          int x,y;
    
          printf("Enter starting point: ");
       scanf("%d",&x);
    
          printf("Enter end point: ");
          scanf("%d",&y);
    
       if(x>ROWS-1 || x<COLS+1 || y>ROWS-1 || y<COLS+1)
       {
      printf("The distance between point x and point y is: %d \n", array[x-1][y-1]);
      return array[x-1][y-1];
       }
          else
          {
       printf("Invalid entry\n"); 
    
          }/*end if*/
    
          return 0;
    
    }/*end display distance*/
    
    void journeyCost()
    {
        float cost;
     cost = dist * 0.4;
     printf("Cost of the journey is: %f\n", cost);
    
    }/*end journey cost*/
    
    void dispChart()
    {
        int x,y;
     for(x=0;x<ROWS;x++)
     {
           printf("\n");
    
         for(y=0;y<COLS;y++)
            {
         printf("\t%d", array[x][y]);
         }/*end for*/
        }/*end for*/
    
     
    
    }/*end dispay chart*/
    any help would be much appreciated.

    Thanks
    I didn't look at the logic behind the code, I just look for any obvious errors.
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by mb86 View Post
    don't think this makes much sense but this is what i have so far:

    Code:
    #include <stdio.h>
    
    #define COST 40
    #define ROWS 10
    #define COLS 10
    
    int menu(void); /* You shouldn't have a prototype for main */
    void dispDistance(void);
    void journeyCost(void);
    void dispChart(void);
    int array[ROWS][COLS]; /* If you can, avoid global variables */
    
    int main(void)
    {
       int choice;
    
       while(choice !='q') /* choice is uninitialized, so it can be any value.
    Yes, even 'q', so your entire loop is skipped. Undefined behavior. */
       {
         switch() /* Missing switch expression here */
         {
          case '1':/*display distance*/
                dispDistance();
          break;
          case '2':/*calculate cost of journey*/
                journeyCost();
          break;
          case '3':/*display the chart*/
                dispChart();
          break;
          default:
              puts("Please enter a valid number");
         }/*end switch*/
    
        }/*end while*/
    
          return 0;
    }
    
    int menu(void)
    {
          int menuChoice;
          puts("Menu");
          /* No such function as Puts. C is CaSe SeNsItIvE */
          Puts("1) Display distance between two places");
          Puts("2) Calculate the cost of a journey");
          Puts("3) Display the full mileage chart");
          Puts("q) Quit\n");
          scanf("&#37;d",&menuChoice);
    
          return choice;
    
    }
    
    void dispDistance() /* Function is missing void in argument list.
    Other functions have it, so why not this one? */
    {
          int x,y;
    
          printf("Enter starting point: ");
       scanf("%d",&x);
    
          printf("Enter end point: ");
          scanf("%d",&y);
    
       if(x>ROWS-1 || x<COLS+1 || y>ROWS-1 || y<COLS+1)
       {
      printf("The distance between point x and point y is: %d \n", array[x-1][y-1]);
      return array[x-1][y-1]; /* Function returns void, so you can't return anything */
       }
          else
          {
       printf("Invalid entry");
    
          }/*end if*/
    
          return 0;
    
    }/*end display distance*/
    
    void journeyCost() /* Another missing void */
    {
        float cost;
     cost = dist * 0.4;
     printf("Cost of the journey is: %f\n", cost);
    
    }/*end journey cost*/
    
    void dispChart() /* Another missing void */
    {
        int x,y;
     for(x=0;x<ROWS;x++)
     {
           printf("\n");
    
         for(y=0;y<COLS;y++)
            {
         printf("\t%d", array[x][y]);
         }/*end for*/
        }/*end for*/
    
     
    
    }/*end dispay chart*/
    any help would be much appreciated.

    Thanks
    Some corrections in red.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    Hi guys, as you can see from the posts above this is the code i have at the minute.

    Code:
    #include <stdio.h>
    
    #define COST 40
    #define ROWS 10
    #define COLS 10
    
    int menu(void);
    void dispDistance(void);
    void journeyCost(void);
    void dispChart(void);
    int array[5][5];
    
    int main(void)
    {
       int choice;
       choice=menu();
       while(choice !='q')
       {
         switch(choice)
         {
          case '1':/*display distance*/
                dispDistance();
          break;
          case '2':/*calculate cost of journey*/
                journeyCost();
          break;
          case '3':/*display the chart*/
                dispChart();
          break;
          default:
              puts("Please enter a valid number");
         }/*end switch*/
        choice=menu();
        }/*end while*/
    
          return 0;
    }
    
    int menu(void)
    {
          int menuChoice;
          puts("Menu");
          puts("1) Display distance between two places");
          puts("2) Calculate the cost of a journey");
          puts("3) Display the full mileage chart");
          puts("q) Quit\n");
          scanf("%d",&menuChoice);
    
          return choice;
    
    }
    
    void dispDistance(void)
    {
          int x,y;
    
          printf("Enter value x: ");
    	  scanf("%d",&x);
    
          printf("Enter value y: ");
          scanf("%d",&y);
    
    	  if(x>ROWS-1 || x<COLS+1 || y>COLS-1 || y<COLS+1)
    	  {
    		printf("The distance between point x and point y is: %d \n", array[x-1][y-1]);
    		return array[x-1][y-1];
    	  }
    	     else
    	     {
    		 printf("Invalid entry");
    
    	     }/*end if*/
    
          return 0;
    
    }/*end display distance*/
    
    void journeyCost(void)
    {
        float cost;
    	cost = dist * 0.4;
    	printf("Cost of the journey is: %f\n", cost);
    
    }/*end journey cost*/
    
    void dispChart()
    {
        int x,y;
    	for(x=0;x<ROWS;x++)
    	{
           printf("\n");
    
    		   for(y=0;y<COLS;y++)
    	       {
    			  printf("\t%d", array[x][y]);
    		   }/*end for*/
        }/*end for*/
    
    
    
    }/*end dispay chart*/
    This code is supposed to:

    1) Initialise: Read data from [the keyboard]
    a) application should allow the population of a chart containing the distances in miles between 10 seperate points. This is entered by the user
    2) Menu: A menu to support the following:
    a) DispChart: Display to the screen a complete mileage chart of the distances of the 10 seperate points entered by the user in grifd form
    b) CalcDistance: Allow input of a Journey between two points and display the distance
    c) Totalcost: Allow input of two points and calculate the cost of the journey at 40ppm
    d) Exit: to exit the program

    As you can probably see the program doesn't do this at the moment and i'm a bit stuck so any help would be much appreciated.

    Thanks.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You've made a big mistake with this generalized "doesn't do this" approach.

    Here's how it works:

    1) You TELL us the SPECIFIC problem you're having.

    2) We make advice based on those SPECIFIC (and perhaps a few general one's too), problem.

    Posting your homework requirements and saying the program doesn't meet it's requirements, doesn't really show you're engaged in this. It shows that you're dumping this here, in the hope somebody will just make it work for you.

    I doubt that will work.

    If you're stuck, what are you stuck on? What is the output of your program that's wrong? Be specific about the problem, the code block or function that's failing, etc.
    Last edited by Adak; 04-20-2008 at 03:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

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