SlideShare a Scribd company logo
1 of 12
Download to read offline
HERE is a C PROGRAM BELOW: EVERYTIME I TRY TO RUN IT ON MATRIX IT GIVES
ME " error: ‘for’ loop initial declarations are only allowed in C99 mode" ERROR. PLEASE
HELP ME TO FIX THIS ERROR (I do not want to run with -std=c99 ,want to edit my program)
#include
#include
struct Box {
int id;
double size[3];
double weight;
};
void listBoxes(const char filename[]);
void printBox(struct Box b);
int searchBox(FILE *fp, int id2Find);
void displayBox(const char filename[], int id2Find);
int addBox(const char filename[], const struct Box * b2Add);
int numberBoxes(const char filename[]); // number of boxes in a file
int getRandomInt(int lower, int higher);
void displayBoxN(char filename[], int n); // displaying a box and it's given its record number
void listBoxes(const char filename[])
{
FILE *fp = NULL;
int a;
double b, c, d, e;
printf("List of boxes ");
printf("=============  ");
printf("ID Length Width Height Weight ");
printf("----------------------------- ");
fp = fopen("storage.txt", "r");
if (fp != NULL)
{
for (int i = 1; i <= 5; i++)
{
fscanf(fp, "%d %lf %lf %lf %lf", &a, &b, &c, &d, &e);
printf(" %2d %6.2lf %5.2lf %6.2lf %6.2lf", a, b, c, d, e);
}
printf(" ");
fclose(fp);
}
else
{
printf("Error ");
}
}
int searchBox(FILE *fp, int id2Find)
{
int a;
double b, c, d, e;
if (fp == NULL)
{
return -1;
}
else
{
rewind(fp);
for (int i = 1; i < 5; i++)
{
fscanf(fp, "%d %lf %lf %lf %lf", &a, &b, &c, &d, &e);
if (id2Find == a)
{
return i;
}
}
return -1;
}
}
void displayBox(const char filename[], int id2Find)
{
FILE *fp = NULL;
struct Box b;
int a = 0;
fp = fopen(filename, "r");
if (fp != NULL)
{
if (a == -1)
{
printf("This box is not recorded. ");
}
else
{
a = searchBox(fp, id2Find);
printf("Found box %d as record #%d: ", id2Find, a);
rewind(fp);
for (int i = 0; i < a; i++) {
fscanf(fp, "%d %lf %lf %lf %lf ", &b.id, &b.size[0], &b.size[1], &b.size[2],
&b.weight);
}
printBox(b);
}
}
else
{
printf("Fail");
}
fclose(fp);
}
void displayRandomBox(const char filename[])
{
FILE *fp = NULL;
struct Box b;
int a = 0;
fp = fopen(filename, "r");
int id2Find=rand() % (5 + 1 - 1) + 1;
if (fp != NULL)
{
a = searchBox(fp, id2Find);
rewind(fp);
for (int i = 0; i < a; i++)
{
fscanf(fp, "%d %lf %lf %lf %lf ", &b.id, &b.size[0], &b.size[1], &b.size[2],
&b.weight);
}
printBox(b);
}
else
{
printf("Fail");
}
fclose(fp);
}
void printBox(struct Box b) {
printf(" ID: %6d "
"Length: %6.2lf "
"Width: %6.2lf "
"Height: %6.2lf "
"Weight: %6.2lf  ", b.id, b.size[0], b.size[1], b.size[2], b.weight);
}
int addBox(const char filename[], const struct Box * b2Add)
{
FILE *fp = NULL;
fp = fopen(filename, "a");
struct Box box;
box = *b2Add;
int c = 0;
if (fp == NULL)
{
printf("Error ");
return 1;
}
else
{
int a = box.id;
int b = searchBox(fp, a);
if (b != -1)
{
printf("A box with this ID is already recorded. ");
}
else
{
fprintf(fp, " %d %lf %lf %lf %lf", box.id, box.size[0], box.size[1], box.size[2],
box.weight);
c = 1;
}
fclose(fp);
}
return c;
}
int menu(void) {
int select = -1;
printf("1- List all boxes ");
printf("2- Find a box ");
printf("3- Add a box ");
printf("4- Randomly pick a lucky box! ");
printf("0- Exit program ");
printf("Select an option: ");
do {
scanf("%d", &select);
if (select < 0 || select > 4)
printf("Please enter a number between 0 and 4: ");
} while (select < 0 || select > 4);
return select;
}
int main(void) {
int choice, boxID, a;
char filename[] = "storage.txt";
struct Box b;
printf("Welcome to My Storage Room ");
printf("========================== ");
do {
choice = menu();
switch (choice) {
case 1:
listBoxes(filename);
break;
case 2:
printf("Enter box ID: ");
scanf("%d", &boxID);
displayBox(filename, boxID);
break;
case 3:
printf("Please enter the box's ID, length, width, height and weight: ");
scanf("%d %lf %lf %lf %lf", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
a = addBox(filename, &b);
printf("%d box added to storage!  ", a);
break;
case 4:
displayRandomBox(filename);
break;
};
} while (choice > 0);
return 0;
}
Solution
/*
in for loop you can't declare int directly.
for(int i=0;i wrong
int i;
for(i=0;i
#include
struct Box {
int id;
double size[3];
double weight;
};
void listBoxes(const char filename[]);
void printBox(struct Box b);
int searchBox(FILE *fp, int id2Find);
void displayBox(const char filename[], int id2Find);
int addBox(const char filename[], const struct Box * b2Add);
int numberBoxes(const char filename[]); // number of boxes in a file
int getRandomInt(int lower, int higher);
void displayBoxN(char filename[], int n); // displaying a box and it's given its record number
void listBoxes(const char filename[])
{
FILE *fp = NULL;
int a;
int i;
double b, c, d, e;
printf("List of boxes ");
printf("=============  ");
printf("ID Length Width Height Weight ");
printf("----------------------------- ");
fp = fopen("storage.txt", "r");
if (fp != NULL)
{
for (i = 1; i <= 5; i++)
{
fscanf(fp, "%d %lf %lf %lf %lf", &a, &b, &c, &d, &e);
printf(" %2d %6.2lf %5.2lf %6.2lf %6.2lf", a, b, c, d, e);
}
printf(" ");
fclose(fp);
}
else
{
printf("Error ");
}
}
int searchBox(FILE *fp, int id2Find)
{
int a,i;
double b, c, d, e;
if (fp == NULL)
{
return -1;
}
else
{
rewind(fp);
for (i = 1; i < 5; i++)
{
fscanf(fp, "%d %lf %lf %lf %lf", &a, &b, &c, &d, &e);
if (id2Find == a)
{
return i;
}
}
return -1;
}
}
void displayBox(const char filename[], int id2Find)
{
FILE *fp = NULL;
struct Box b;
int a = 0,i;
fp = fopen(filename, "r");
if (fp != NULL)
{
if (a == -1)
{
printf("This box is not recorded. ");
}
else
{
a = searchBox(fp, id2Find);
printf("Found box %d as record #%d: ", id2Find, a);
rewind(fp);
for (i = 0; i < a; i++) {
fscanf(fp, "%d %lf %lf %lf %lf ", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
}
printBox(b);
}
}
else
{
printf("Fail");
}
fclose(fp);
}
void displayRandomBox(const char filename[])
{
FILE *fp = NULL;
struct Box b;
int a = 0,i;
fp = fopen(filename, "r");
int id2Find=rand() % (5 + 1 - 1) + 1;
if (fp != NULL)
{
a = searchBox(fp, id2Find);
rewind(fp);
for (i = 0; i < a; i++)
{
fscanf(fp, "%d %lf %lf %lf %lf ", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
}
printBox(b);
}
else
{
printf("Fail");
}
fclose(fp);
}
void printBox(struct Box b) {
printf(" ID: %6d "
"Length: %6.2lf "
"Width: %6.2lf "
"Height: %6.2lf "
"Weight: %6.2lf  ", b.id, b.size[0], b.size[1], b.size[2], b.weight);
}
int addBox(const char filename[], const struct Box * b2Add)
{
FILE *fp = NULL;
fp = fopen(filename, "a");
struct Box box;
box = *b2Add;
int c = 0;
if (fp == NULL)
{
printf("Error ");
return 1;
}
else
{
int a = box.id;
int b = searchBox(fp, a);
if (b != -1)
{
printf("A box with this ID is already recorded. ");
}
else
{
fprintf(fp, " %d %lf %lf %lf %lf", box.id, box.size[0], box.size[1], box.size[2], box.weight);
c = 1;
}
fclose(fp);
}
return c;
}
int menu(void) {
int select = -1;
printf("1- List all boxes ");
printf("2- Find a box ");
printf("3- Add a box ");
printf("4- Randomly pick a lucky box! ");
printf("0- Exit program ");
printf("Select an option: ");
do {
scanf("%d", &select);
if (select < 0 || select > 4)
printf("Please enter a number between 0 and 4: ");
} while (select < 0 || select > 4);
return select;
}
int main(void) {
int choice, boxID, a;
char filename[] = "storage.txt";
struct Box b;
printf("Welcome to My Storage Room ");
printf("========================== ");
do {
choice = menu();
switch (choice) {
case 1:
listBoxes(filename);
break;
case 2:
printf("Enter box ID: ");
scanf("%d", &boxID);
displayBox(filename, boxID);
break;
case 3:
printf("Please enter the box's ID, length, width, height and weight: ");
scanf("%d %lf %lf %lf %lf", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
a = addBox(filename, &b);
printf("%d box added to storage!  ", a);
break;
case 4:
displayRandomBox(filename);
break;
};
} while (choice > 0);
return 0;
}

More Related Content

Similar to HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdf

2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt2. Data, Operators, IO.ppt
2. Data, Operators, IO.pptswateerawat06
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
#include stdio.h#include stdlib.htypedef FILE stream.docx
#include stdio.h#include stdlib.htypedef FILE stream.docx#include stdio.h#include stdlib.htypedef FILE stream.docx
#include stdio.h#include stdlib.htypedef FILE stream.docxkatherncarlyle
 
C programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfC programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfbadshetoms
 
C언어 스터디 강의자료 - 1차시
C언어 스터디 강의자료 - 1차시C언어 스터디 강의자료 - 1차시
C언어 스터디 강의자료 - 1차시Junha Jang
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfezzi552
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Slide set 6 Strings and pointers.pdf
Slide set 6 Strings and pointers.pdfSlide set 6 Strings and pointers.pdf
Slide set 6 Strings and pointers.pdfHimanshuKansal22
 
a) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdfa) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdfanuradhasilks
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c languageHarish Gyanani
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 

Similar to HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdf (20)

2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
#include stdio.h#include stdlib.htypedef FILE stream.docx
#include stdio.h#include stdlib.htypedef FILE stream.docx#include stdio.h#include stdlib.htypedef FILE stream.docx
#include stdio.h#include stdlib.htypedef FILE stream.docx
 
C programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfC programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdf
 
Tu1
Tu1Tu1
Tu1
 
C언어 스터디 강의자료 - 1차시
C언어 스터디 강의자료 - 1차시C언어 스터디 강의자료 - 1차시
C언어 스터디 강의자료 - 1차시
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Function recap
Function recapFunction recap
Function recap
 
Function recap
Function recapFunction recap
Function recap
 
7 functions
7  functions7  functions
7 functions
 
Slide set 6 Strings and pointers.pdf
Slide set 6 Strings and pointers.pdfSlide set 6 Strings and pointers.pdf
Slide set 6 Strings and pointers.pdf
 
a) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdfa) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdf
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c language
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
pointers 1
pointers 1pointers 1
pointers 1
 
Functions
FunctionsFunctions
Functions
 
c programming
c programmingc programming
c programming
 

More from fashiongallery1

For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdffashiongallery1
 
Discuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdfDiscuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdffashiongallery1
 
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdfDiscrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdffashiongallery1
 
Describe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdfDescribe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdffashiongallery1
 
Describe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdfDescribe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdffashiongallery1
 
Can someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdfCan someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdffashiongallery1
 
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdfAssume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdffashiongallery1
 
All computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdfAll computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdffashiongallery1
 
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdffashiongallery1
 
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdfBriefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdffashiongallery1
 
Write a class that implements the BagInterface. BagInterface should .pdf
Write a class that implements the BagInterface.  BagInterface should .pdfWrite a class that implements the BagInterface.  BagInterface should .pdf
Write a class that implements the BagInterface. BagInterface should .pdffashiongallery1
 
write an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdfwrite an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdffashiongallery1
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdffashiongallery1
 
Why are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdfWhy are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdffashiongallery1
 
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdfWhich of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdffashiongallery1
 
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdfWhat made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdffashiongallery1
 
What data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdfWhat data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdffashiongallery1
 
1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdffashiongallery1
 
VERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdfVERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdffashiongallery1
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdfUsing standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdffashiongallery1
 

More from fashiongallery1 (20)

For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
 
Discuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdfDiscuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdf
 
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdfDiscrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
 
Describe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdfDescribe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdf
 
Describe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdfDescribe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdf
 
Can someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdfCan someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdf
 
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdfAssume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
 
All computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdfAll computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdf
 
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
 
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdfBriefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
 
Write a class that implements the BagInterface. BagInterface should .pdf
Write a class that implements the BagInterface.  BagInterface should .pdfWrite a class that implements the BagInterface.  BagInterface should .pdf
Write a class that implements the BagInterface. BagInterface should .pdf
 
write an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdfwrite an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdf
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
 
Why are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdfWhy are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdf
 
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdfWhich of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
 
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdfWhat made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
 
What data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdfWhat data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdf
 
1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf
 
VERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdfVERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdf
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdfUsing standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
 

Recently uploaded

How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 

Recently uploaded (20)

How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 

HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdf

  • 1. HERE is a C PROGRAM BELOW: EVERYTIME I TRY TO RUN IT ON MATRIX IT GIVES ME " error: ‘for’ loop initial declarations are only allowed in C99 mode" ERROR. PLEASE HELP ME TO FIX THIS ERROR (I do not want to run with -std=c99 ,want to edit my program) #include #include struct Box { int id; double size[3]; double weight; }; void listBoxes(const char filename[]); void printBox(struct Box b); int searchBox(FILE *fp, int id2Find); void displayBox(const char filename[], int id2Find); int addBox(const char filename[], const struct Box * b2Add); int numberBoxes(const char filename[]); // number of boxes in a file int getRandomInt(int lower, int higher); void displayBoxN(char filename[], int n); // displaying a box and it's given its record number void listBoxes(const char filename[]) { FILE *fp = NULL; int a; double b, c, d, e; printf("List of boxes "); printf("============= "); printf("ID Length Width Height Weight "); printf("----------------------------- "); fp = fopen("storage.txt", "r"); if (fp != NULL) { for (int i = 1; i <= 5; i++) { fscanf(fp, "%d %lf %lf %lf %lf", &a, &b, &c, &d, &e); printf(" %2d %6.2lf %5.2lf %6.2lf %6.2lf", a, b, c, d, e);
  • 2. } printf(" "); fclose(fp); } else { printf("Error "); } } int searchBox(FILE *fp, int id2Find) { int a; double b, c, d, e; if (fp == NULL) { return -1; } else { rewind(fp); for (int i = 1; i < 5; i++) { fscanf(fp, "%d %lf %lf %lf %lf", &a, &b, &c, &d, &e); if (id2Find == a) { return i; } } return -1; } } void displayBox(const char filename[], int id2Find) { FILE *fp = NULL; struct Box b; int a = 0;
  • 3. fp = fopen(filename, "r"); if (fp != NULL) { if (a == -1) { printf("This box is not recorded. "); } else { a = searchBox(fp, id2Find); printf("Found box %d as record #%d: ", id2Find, a); rewind(fp); for (int i = 0; i < a; i++) { fscanf(fp, "%d %lf %lf %lf %lf ", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight); } printBox(b); } } else { printf("Fail"); } fclose(fp); } void displayRandomBox(const char filename[]) { FILE *fp = NULL; struct Box b; int a = 0; fp = fopen(filename, "r"); int id2Find=rand() % (5 + 1 - 1) + 1; if (fp != NULL) { a = searchBox(fp, id2Find);
  • 4. rewind(fp); for (int i = 0; i < a; i++) { fscanf(fp, "%d %lf %lf %lf %lf ", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight); } printBox(b); } else { printf("Fail"); } fclose(fp); } void printBox(struct Box b) { printf(" ID: %6d " "Length: %6.2lf " "Width: %6.2lf " "Height: %6.2lf " "Weight: %6.2lf ", b.id, b.size[0], b.size[1], b.size[2], b.weight); } int addBox(const char filename[], const struct Box * b2Add) { FILE *fp = NULL; fp = fopen(filename, "a"); struct Box box; box = *b2Add; int c = 0; if (fp == NULL) { printf("Error "); return 1; } else { int a = box.id;
  • 5. int b = searchBox(fp, a); if (b != -1) { printf("A box with this ID is already recorded. "); } else { fprintf(fp, " %d %lf %lf %lf %lf", box.id, box.size[0], box.size[1], box.size[2], box.weight); c = 1; } fclose(fp); } return c; } int menu(void) { int select = -1; printf("1- List all boxes "); printf("2- Find a box "); printf("3- Add a box "); printf("4- Randomly pick a lucky box! "); printf("0- Exit program "); printf("Select an option: "); do { scanf("%d", &select); if (select < 0 || select > 4) printf("Please enter a number between 0 and 4: "); } while (select < 0 || select > 4); return select; } int main(void) { int choice, boxID, a; char filename[] = "storage.txt"; struct Box b; printf("Welcome to My Storage Room ");
  • 6. printf("========================== "); do { choice = menu(); switch (choice) { case 1: listBoxes(filename); break; case 2: printf("Enter box ID: "); scanf("%d", &boxID); displayBox(filename, boxID); break; case 3: printf("Please enter the box's ID, length, width, height and weight: "); scanf("%d %lf %lf %lf %lf", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight); a = addBox(filename, &b); printf("%d box added to storage! ", a); break; case 4: displayRandomBox(filename); break; }; } while (choice > 0); return 0; } Solution /* in for loop you can't declare int directly. for(int i=0;i wrong
  • 7. int i; for(i=0;i #include struct Box { int id; double size[3]; double weight; }; void listBoxes(const char filename[]); void printBox(struct Box b); int searchBox(FILE *fp, int id2Find); void displayBox(const char filename[], int id2Find); int addBox(const char filename[], const struct Box * b2Add); int numberBoxes(const char filename[]); // number of boxes in a file int getRandomInt(int lower, int higher); void displayBoxN(char filename[], int n); // displaying a box and it's given its record number void listBoxes(const char filename[]) { FILE *fp = NULL; int a; int i; double b, c, d, e; printf("List of boxes "); printf("============= "); printf("ID Length Width Height Weight "); printf("----------------------------- "); fp = fopen("storage.txt", "r"); if (fp != NULL) { for (i = 1; i <= 5; i++) { fscanf(fp, "%d %lf %lf %lf %lf", &a, &b, &c, &d, &e); printf(" %2d %6.2lf %5.2lf %6.2lf %6.2lf", a, b, c, d, e); } printf(" ");
  • 8. fclose(fp); } else { printf("Error "); } } int searchBox(FILE *fp, int id2Find) { int a,i; double b, c, d, e; if (fp == NULL) { return -1; } else { rewind(fp); for (i = 1; i < 5; i++) { fscanf(fp, "%d %lf %lf %lf %lf", &a, &b, &c, &d, &e); if (id2Find == a) { return i; } } return -1; } } void displayBox(const char filename[], int id2Find) { FILE *fp = NULL; struct Box b; int a = 0,i; fp = fopen(filename, "r"); if (fp != NULL)
  • 9. { if (a == -1) { printf("This box is not recorded. "); } else { a = searchBox(fp, id2Find); printf("Found box %d as record #%d: ", id2Find, a); rewind(fp); for (i = 0; i < a; i++) { fscanf(fp, "%d %lf %lf %lf %lf ", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight); } printBox(b); } } else { printf("Fail"); } fclose(fp); } void displayRandomBox(const char filename[]) { FILE *fp = NULL; struct Box b; int a = 0,i; fp = fopen(filename, "r"); int id2Find=rand() % (5 + 1 - 1) + 1; if (fp != NULL) { a = searchBox(fp, id2Find); rewind(fp); for (i = 0; i < a; i++) { fscanf(fp, "%d %lf %lf %lf %lf ", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight);
  • 10. } printBox(b); } else { printf("Fail"); } fclose(fp); } void printBox(struct Box b) { printf(" ID: %6d " "Length: %6.2lf " "Width: %6.2lf " "Height: %6.2lf " "Weight: %6.2lf ", b.id, b.size[0], b.size[1], b.size[2], b.weight); } int addBox(const char filename[], const struct Box * b2Add) { FILE *fp = NULL; fp = fopen(filename, "a"); struct Box box; box = *b2Add; int c = 0; if (fp == NULL) { printf("Error "); return 1; } else { int a = box.id; int b = searchBox(fp, a); if (b != -1) { printf("A box with this ID is already recorded. "); }
  • 11. else { fprintf(fp, " %d %lf %lf %lf %lf", box.id, box.size[0], box.size[1], box.size[2], box.weight); c = 1; } fclose(fp); } return c; } int menu(void) { int select = -1; printf("1- List all boxes "); printf("2- Find a box "); printf("3- Add a box "); printf("4- Randomly pick a lucky box! "); printf("0- Exit program "); printf("Select an option: "); do { scanf("%d", &select); if (select < 0 || select > 4) printf("Please enter a number between 0 and 4: "); } while (select < 0 || select > 4); return select; } int main(void) { int choice, boxID, a; char filename[] = "storage.txt"; struct Box b; printf("Welcome to My Storage Room "); printf("========================== "); do { choice = menu(); switch (choice) { case 1: listBoxes(filename);
  • 12. break; case 2: printf("Enter box ID: "); scanf("%d", &boxID); displayBox(filename, boxID); break; case 3: printf("Please enter the box's ID, length, width, height and weight: "); scanf("%d %lf %lf %lf %lf", &b.id, &b.size[0], &b.size[1], &b.size[2], &b.weight); a = addBox(filename, &b); printf("%d box added to storage! ", a); break; case 4: displayRandomBox(filename); break; }; } while (choice > 0); return 0; }