This is for a C-programming class. Please send back a code for C that will do th
ID: 3831239 • Letter: T
Question
This is for a C-programming class. Please send back a code for C that will do these functions.
Your program should offer the following menu to the user:
1) Print Author name and ID
2) Enter integer value
3) Enter floating point value
4) Print stored values
5) Clear integers
6) Clear floating points
7) Save data
8) Load data
0) Exit
Where each option does the following:
1 Print your name and id
2 Prompts the user to enter an integer value, you then store this value in memory (program should handle up to 500 values)
3 Prompts the user to enter a floating point value, you then store this value in memory (program should handle up to 500 values)
4 Prints out all of the stored values (more on this later)
5) Clears stored integers
6) Clears stored floating point values
7) Saves the current stored values to a file (file name specified by the user)
8) Sets the current stored values to the data stored in a file (file name specified by user)
Also: your program should note that the application has started, and keep a running log of any errors (attempts to open files that fail and invalid
selections chosen)
When a log entry is made you should print your student id, followed by an error message describing the problem.
These log entries should be saved to a file called "log.txt" and theh program should never cause the program to erase previous entries.
So if you open the program, make an invalid selection, close the program, open it again then your log should contain the following info:
program started
invalid selection
program started
How you can accomplish not deleting the file when you open it was not discussed in class, however, should be easy to figure out what to do using google
hint: the solution is very easy, if whatever solution you find takes more than 1 second to implement, it's probably not what you want.
Notes:
For option 4, you should print your data on two lines with the following prefix values:
"Integers: "
"Floating Points: "
For option 8, your program needs to be able to load files that your program generated with option 7, it does not need to be able to load files from other students
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#define SIZE 500
#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif
#include <math.h>
int menu();
int main()
{
int done, choice, numbers;
float floatNumbers[SIZE], floatValue;
int intNumbers[SIZE], intCount=0,floatCount=0,intValue;
char * name = "<Your Name>";
char * id = "<Your id>";
FILE* file;
done=FALSE;
while(!done){
choice = menu();
switch(choice){
//////////////////////////////////////////////////////////////////
case 1:
printf(" Name : %s ID: %s ",name,id);
break;
///////////////////////////////////////////////////////////////////
case 2:
printf(" Enter one integer Value :");
scanf("%d",&intValue);
if(intCount<SIZE){
intNumbers[intCount] = intValue;
intCount++;
}
break;
///////////////////////////////////////////////////////////////////
case 3:
printf(" Enter one float Value :");
scanf("%f",&floatValue);
if(floatCount<SIZE){
floatNumbers[floatCount] = floatValue;
floatCount++;
}
break;
///////////////////////////////////////////////////////////////////
case 4:
int i;
printf(" Integers :");
for (i=0; i < intCount; ++i){
printf(" %d ",intNumbers[i]);
}
printf(" Floats :");
for (i=0; i < floatCount; ++i){
printf(" %f ",floatNumbers[i]);
}
printf(" ");
break;
///////////////////////////////////////////////////////////////////
case 5:
intCount=0;
break;
///////////////////////////////////////////////////////////////////
case 6:
floatCount=0;
break;
///////////////////////////////////////////////////////////////////
case 7:
file = fopen ("float_numbers", "w");
if(file == NULL){
printf(" Not able to open float file ");
break;
}
int count;
count = 0;
while (count < floatCount)
{
fprintf(file,"%f ",floatNumbers[count]);
count++;
}
file = fopen ("int_numbers", "w");
if(file == NULL){
printf(" Not able to open int file ");
break;
}
count = 0;
while (count < intCount)
{
fprintf(file,"%f ",intNumbers[count]);
count++;
}
break;
////////////////////////////////////////////////////////////////////
case 8:
file = fopen ("float_numbers", "r");
if(file == NULL){
printf(" Not able to open float file ");
break;
}
floatCount = 0;
while (!feof (file))
{
fprintf(file,"%f ",floatNumbers[floatCount]);
floatCount++;
}
file = fopen ("int_numbers", "r");
if(file == NULL){
printf(" Not able to open int file ");
break;
}
intCount = 0;
while (!feof (file))
{
fprintf(file,"%f ",intNumbers[intCount]);
intCount++;
}
break;
////////////////////////////////////////////////////////////////////
case 0:
puts("Good bye!");//exit
done=TRUE;
break;
}
}
return 0;
}
int menu(){
int choice;
while(1){
printf("Select the desired opersation: ");
printf(" 1 Print Author name and ID? ");
printf(" 2 Enter integer value ");
printf(" 3 Enter floating point value ");
printf(" 4 Print stored values ");
printf(" 5 Clear integers ");
printf(" 6 Clear floating points ");
printf(" 7 Save data ");
printf(" 8 Load data ");
printf(" 0 Exit ");
printf("choice? ");
scanf("%d",&choice);
if(choice<0 || choice>8){
printf("Invalid Choice, try again! ");
continue;
}
break;
}
return choice;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.