// C Programming You are to add the missing functions in provide.c. When complet
ID: 3705323 • Letter: #
Question
// C Programming
You are to add the missing functions in provide.c. When completed the program must support the following features.
When the program starts ask for the user's name (3 words) it must then ask how many integers and doubles the program should support (it must be able to handle up to that many values) it will then present a menu to the user
the menu options are:
1 - Author info
2 = Enter integer
3 - Enter double
4 - Clear values
5 - Print values
6 - Sum integers
7 - Sum doubles
0 - Exit
Option 0 must exit the program
Option 1 must print information about the program, this must include the currently logged in user, the name and id of the author, as well as your author code which is: 377
it must also print how many characters were printed to the screen for the listed data
Option 2 must take in 1 integer and store it into the program's memory
Option 3 must take in 1 double and store it into the program's memory
Option 4 must clear entered values.
Option 5 must print all entered integers and doubles.
Option 6 must print the sum of all entered integers
Option 7 must print the sum of all entered doubles.
----------------------------------------------------------------------------------------------------------------------
#include
#include
#include
//do not edit anyting below this line (your changes will not be included when grading)
int main()
{
int maxint, maxdouble;
char name[512];
int sel;
int *ints;
int icount = 0, dcount = 0;
double *doubles;
setUsername(name);
ints = initializeInts(&maxint);
doubles = initializeDoubles(&maxdouble);
int len;
while(1)
{
printf(" Welcome %s 0) Exit 1) Author Info 2) Enter int 3) Enter doubles 4) Clear values 5) Print values 6) Sum Integers 7) Sum doubles ", name);
scanf("%d", &sel);
if (sel == 0)
{
break;
}
else if (sel == 1)
{
len = printProgramInfo(name);
printf("Info data: %d ", len);
}
else if (sel == 2)
{
if (icount == maxint)
{
printf("Already at max values, clear values to continue ");
continue;
}
enterInt(ints, &icount);
}
else if (sel == 3)
{
if (dcount == maxdouble)
{
printf("Already at max values, clear values to continue ");
continue;
}
dcount = enterDouble(doubles,dcount);
}
else if (sel == 4)
{
icount = dcount = 0;
}
else if (sel == 5)
{
printVals(ints,icount,doubles,dcount);
}
else if (sel == 6)
{
printf("Sum of ints: %d ", sumInts(ints, icount));
}
else if (sel == 7)
{
printf("Sum of doubles: %lf ", sumDoubles(doubles, dcount));
}
else
{
printf("Invalid Selection ");
}
}
return(0);
}
Explanation / Answer
========================================================================================
Note: Note clear about Option 1:
In question it mentioned that "program starts ask for the user's name (3 words)", In Option 1, we should print username, name , Author ID. Are 3 words are these 3 words ?
Please reach us for any clarification.
======================================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void setUsername(char *name) {
// Assuming User enter " username NAME id"
printf(" Enter your Username, Name, Id (3 words with spaces): ");
fgets(name, 512, stdin);
}
int* initializeInts(int* maxint) {
int num=0;
printf(" Enter Max Number of integers: ");
scanf("%d", &num);
*maxint = num; //copy the read value to maxint
int* iptr = (int *)malloc(num * sizeof(int));
return iptr;
}
double* initializeDoubles(int* maxdouble) {
int num=0;
printf(" Enter Max Number of Doubles: ");
scanf("%d", &num);
*maxdouble = num;
double* dptr = (double *)malloc(num * sizeof(double));
return dptr;
}
int printProgramInfo(char *name) {
int pinfo;
int len = strlen(name); // Calculating string length of the user entered string.
len = len + strlen("Author Code: 377"); // You can comment this line if Author code is not required to count.
printf(" The Program Info: %s, Author Code: 377 ",name);
//printf(" Total number of character printed: %d", len);
return len;
}
void enterInt(int* ints, int *icount) {
int num;
printf(" Enter Integer Value: ");
scanf("%d", &num);
*(ints+(*icount)) = num; //Insert value to the next available location
(*icount)++; // Increment the counter
}
int enterDouble(double* doubles, int dcount) {
double num;
printf(" Enter Double value: ");
scanf("%lf",&num);
*(doubles+dcount) = num; //Insert value to the next available location
dcount++; // Increment the counter
return dcount;
}
void printVals(int* ints, int icount, double* doubles, int dcount) {
int i = 0;
printf(" The Integer values are: ");
for (i = 0; i < icount; i++)
printf("%d ", ints[i]);
printf(" The Double values are: ");
for (i = 0; i < dcount; i++)
printf("%lf ", doubles[i]);
}
int sumInts(int* ints, int icount) {
int sum = 0, i = 0;
for (i = 0; i < icount; i++)
sum = sum + ints[i];
return sum;
}
double sumDoubles(double* doubles, int dcount) {
double sum = 0;
int i = 0;
for (i = 0; i < dcount; i++)
sum = sum + doubles[i];
return sum;
}
//do not edit anyting below this line (your changes will not be included when grading)
//do not edit anyting below this line (your changes will not be included when grading)
int main()
{
int maxint, maxdouble;
char name[512];
int sel;
int *ints;
int icount = 0, dcount = 0;
double *doubles;
setUsername(name);
ints = initializeInts(&maxint);
doubles = initializeDoubles(&maxdouble);
int len;
while (1)
{
printf(" Welcome %s 0) Exit 1) Author Info 2) Enter int 3) Enter doubles 4) Clear values 5) Print values 6) Sum Integers 7) Sum doubles ", name);
scanf("%d", &sel);
if (sel == 0)
{
break;
}
else if (sel == 1)
{
len = printProgramInfo(name);
printf("Info data: %d ", len);
}
else if (sel == 2)
{
if (icount == maxint)
{
printf("Already at max values, clear values to continue ");
continue;
}
enterInt(ints, &icount);
}
else if (sel == 3)
{
if (dcount == maxdouble)
{
printf("Already at max values, clear values to continue ");
continue;
}
dcount = enterDouble(doubles, dcount);
}
else if (sel == 4)
{
icount = dcount = 0;
}
else if (sel == 5)
{
printVals(ints, icount, doubles, dcount);
}
else if (sel == 6)
{
printf("Sum of ints: %d ", sumInts(ints, icount));
}
else if (sel == 7)
{
printf("Sum of doubles: %lf ", sumDoubles(doubles, dcount));
}
else
{
printf("Invalid Selection ");
}
}
return(0);
}
==============================================================
Sample Output:
C:eclipseprojdevc>a.exe
Enter your Username, Name, Id (3 words with spaces): mike MikeTyson 1234
String: mike MikeTyson 1234
Enter Max Number of integers: 2
Enter Max Number of Doubles: 2
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
1
The Program Info: mike MikeTyson 1234
, Author Code: 377 Info data: 36
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
2
Enter Integer Value: 25
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
2
Enter Integer Value: 35
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
2
Already at max values, clear values to continue
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
5
The Integer values are: 25 35
The Double values are:
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
6
Sum of ints: 60
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
3
Enter Double value: 32.7
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
5
The Integer values are: 25 35
The Double values are: 32.700000
Welcome mike MikeTyson 1234
0) Exit
1) Author Info
2) Enter int
3) Enter doubles
4) Clear values
5) Print values
6) Sum Integers
7) Sum doubles
0
C:eclipseprojdevc>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.