Question
Hi the programming language for this assignment is C programming. Could you also please number the answers exactly as they appear in the assignment? Thank you.

(9) 1. An array of structures is needed to keep track of 50 grocery items containing the item name, item type, cost, quantity
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Find the source code below. I have tested the code so i have provided lines for printing for testing purpose as well.They are commented out. Uncomment and you see result.Comment if any doubt.

Note - Create input file ITEMS.txt and place it in same folder. See screenshot.

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

typedef struct grocery{
char name[20],type[15];
float cost,taxp;
int quan;
}grocery;
int main(void) {
grocery g[50];

/************part a ***************/
FILE* file;
if((file=fopen("ITEMS.txt","r"))==NULL){
    printf("Error in opening file");
    exit(1);
}
int count=0;
while(
    fscanf(file,"%s %s %f %d %f",g[count].name,g[count].type,&g[count].cost,&g[count].quan,&g[count].taxp)!=EOF){
    //printf("%s|%s|%f|%d|%f\n",g[count].name,g[count].type,g[count].cost,g[count].quan,g[count].taxp);
    count++;
}

/************part b ***************/
//sort
int i;
for(i=1;i<count;i++){
    int j=1;
    while(j>=1 && g[j].cost>g[j-1].cost){
      grocery temp=g[j-1];
      g[j-1]=g[j];
      g[j]=temp;
    }
}
/*
//print sorted list
printf("printing sorted list\n");
for(i=0;i<count;i++){
    printf("%s|%s|%f|%d|%f\n",g[i].name,g[i].type,g[i].cost,g[i].quan,g[i].taxp);
}*/

/*********part c**********/
FILE* fw;
if((fw=fopen("ITEMS.bin","w"))==NULL){
    printf("can not open file to write");
}
fwrite(g,sizeof(grocery),count,fw);
grocery f[50];
fclose(fw);
/*printf("read from file\n");
FILE* fr=fopen("ITEMS.bin","r");
fread(f,sizeof(grocery),count,fr);
for(i=0;i<count;i++){
    printf("%s|%s|%f|%d|%f\n",f[i].name,f[i].type,f[i].cost,f[i].quan,f[i].taxp);
}*/

return 0;
}

==================================Sample input file=====

ITEMS.txt

Apple
Fruit
12.34 12 10.5
Milk
Drinks
24 2 5

=======================================

Files clang-7 -pthread -lm -o main main.c ./main с main.c L ITEMS.bin L ITEMS.txt } main.c saved 5 char name[20], type[15]; 6

Add a comment
Know the answer?
Add Answer to:
Hi the programming language for this assignment is C programming. Could you also please number the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1)...

    Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1) (1) Create three files to submit: • Item ToPurchase.h - Struct definition and related function declarations • Item ToPurchase.c-Related function definitions • main.c-main function Build the ItemToPurchase struct with the following specifications: • Data members (3 pts) • char itemName • int itemPrice • int itemQuantity • Related functions • MakeltemBlank0 (2 pts) Has a pointer to an item To Purchase parameter. Sets item's...

  • In C++ Programming Write a program in Restaurant.cpp to help a local restaurant automate its breakfast...

    In C++ Programming Write a program in Restaurant.cpp to help a local restaurant automate its breakfast billing system. The program should do the following: Show the customer the different breakfast items offered by the restaurant. Allow the customer to select more than one item from the menu. Calculate and print the bill. Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item): Name Price Egg (cooked to order)...

  • (Write a program, and show the output too) C programming I have a hard time to...

    (Write a program, and show the output too) C programming I have a hard time to get these five questions done. I am thankful if someone can help me with that. I started with something but i couldn't get it done. That's what I've done. #include <stdio.h> typedef struct _profile {    char name[32];    int age; } Profile; Profile prof[3] = {"myName", 19}, {"bob", 12} arr[1] = prof; for (int i = 0; i < 3; i++) {   ...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • please use the c language Assignment 12 The program to write in this assignment is a...

    please use the c language Assignment 12 The program to write in this assignment is a program that calculates score statistics and manages the grades of the students. First, the student list and are given in a file named "student.dat" as in the following: 이성우 77 홍길동 88 scores 201 1710086 2012700091 This program reads the input file "student.dat" and keeps this data in a linear linked list. Each node must be a struct which contains the following: student id...

  • programming language is C++. Please also provide an explanation of how to create a file with...

    programming language is C++. Please also provide an explanation of how to create a file with the given information and how to use that file in the program Let's consider a file with the following student information: John Smith 99.0 Sarah Johnson 85.0 Jim Robinson 70.0 Mary Anderson 100.0 Michael Jackson 92.0 Each line in the file contains the first name, last name, and test score of a student. Write a program that prompts the user for the file name...

  • Code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; ...

    code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; Specification: In this lab, five functions need to be implemented using the given ADT. 1. Employee* readRecord(FILE*) This function receives a FILE pointer created before. It reads a line from the provided csv file, and creates an Employee struct pointer with the information from that line then returns the pointer back to the calling function. Each line in the provided csv file contains the id, salary,...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • The Code need to be in C# programming language. could you please leave some explanation notes...

    The Code need to be in C# programming language. could you please leave some explanation notes as much as you can? Thank you Create the following classes in the class library with following attributes: 1. Customer a. id number – customer’s id number b. name –the name of the customer c. address – address of the customer (use structs) d. telephone number – 10-digit phone number e. orders – collection (array) of orders Assume that there will be no more...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT