Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program (File Processing - Writing) The same Bicycle shop that contracted you

ID: 3776173 • Letter: C

Question

C program

(File Processing - Writing) The same Bicycle shop that contracted you to write the double array problem in HW5 has now decided that they want to keep a log of all employee transactions. Write a program that will allow users to enter in their name (you only need to use first name) and the item that they sold (for example, “James Bicycle”). After each employee types in a sale, your program should record that sale in a file called sales.txt. Each sale should comprise one line of the sales.txt file (thus, be sure to use a after each sale). At the end of the day when the manager types in „quit quit the program should terminate.

HW5

(Two-dimension Array) A company that pays their employees on a commission basis has contracted you to write a program that will calculate how much they owe their employees. The company has 4 employees and sells 5 different products. The products and the commission each employee receives from selling the products is given below: 1. bicycle - $20 2. unicycle - $10 3. tire - $5 4. pump - $2 5. chain - $1

Explanation / Answer

// question1

// this programme overwrites the file everytime it is executed

// for standard input printf,scanf etc

#include<stdio.h>

// for including bool values
#include<stdbool.h>

//for including string functionalitites
#include<string.h>

//compare whether strings are equal or not
bool stringcmp(char str1[],char str2[])
{
   // len for storing current char position
   int len=0;
   // as long as str1 does not end keep on running loop
   while(str1[len]!='')
   {
       // as long as element of str1 is equal to str2 keep on increasing len value for iteration
       if(str1[len]==str2[len])
           len++;
       // break if mismatch
       else
           break;
   }
   // is str1 is parsed totally return true
   if(str1[len]=='')
       return true;
   // return false if str1 as both strings are not equal
   return false;
}

int main()
{
   // fp = file descriptor
   FILE *fp;
   // line to store line string from file
   char line[20];
   // open file "sales.txt" for writing
   fp=fopen("sales.txt","w");
   // keep on running continously
   while(true)
   {
       // ask user for input
       printf("enter name product: ");
       // flush stdin to clear stdin buffer
       fflush(stdin);
       // get input from stdin(keyboard) and store in line
       fgets(line,20,stdin);
       // change 2nd last element to as it will be containing ' '
       line[strlen(line)-1]='';
       // if string from input is 'exit'
       if(stringcmp(line,"exit"))
       {
       //end loop
           break;
       }
       // enter line to file
       fprintf(fp,"%s ",line);
   }
   // close file
   close(fp);
   return 0;
}

//answer2

#include<stdio.h>

int main()
{
   //declare integer i for loop iteration , total for calculating total commision
   int i,total;
   //interger variables to store total sales
   int bicyle,unicycle,tire,pump,chain;
   // integer 2-d array for storing values
   int comm[4][5];
   //read sales of each employee one by one
   for(i=0;i<4;i++)
   {
       // ask for input for a employee and take input from keyboard and store by rows
       printf("enter employee %d sales: ",i+1);
       printf("enter no. of bicycles: ");
       scanf("%d",&comm[i][0]);
       printf("enter no. of unicycles: ");
       scanf("%d",&comm[i][1]);
       printf("enter no. of tire: ");
       scanf("%d",&comm[i][2]);
       printf("enter no. of chain: ");
       scanf("%d",&comm[i][3]);
       printf("enter no. of pump:");
       scanf("%d",&comm[i][4]);
      
       // multiply each sales item of employee with price of item
       comm[i][0]*=20;
       comm[i][1]*=10;
       comm[i][2]*=5;
       comm[i][3]*=1;
       comm[i][4]*=2;
      
   }

   printf(" ");
   // print commision of each employee  
   for(i=0;i<4;i++)
   {
       //add all the values from current row and store in total
       total=comm[i][0] + comm[i][1] + comm[i][2] + comm[i][3] + comm[i][4];
       // print total commision
       printf("enter employee %d commision: %d ",i+1,total);
   }
   return 0;
}