Question

Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified.

Source code:

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


int main(void) {
char title[50];
char col1[50];
char col2[50];
int point[50];
char names[50][50];
printf("Enter a title for the data:\n");
fgets (title, 50, stdin);
printf("You entered: %s\n", title);
printf("Enter the column 1 header:\n");
fgets (col1, 50, stdin);
printf("You entered: %s\n", col1);
printf("Enter the column 2 header:\n");
fgets (col2, 50, stdin);
printf("You entered: %s\n", col2);
col1[strlen(col1) - 1] = '\0';
col2[strlen(col2) - 1] = '\0';
int count = 0;
char dataPoint[50];
while (count < 50) {
printf("Enter a data point(-1 to stop input):\n");
fgets (dataPoint, 50, stdin);
if (atoi(dataPoint) == -1) {
exit(0);
}
int commas = 0;
int i = 0;
int intFound = 0;
char integerValue[5] = "";
int commaAfterInt = 0;
  
while (dataPoint[i] != '\0') {
if (dataPoint[i] == ',') {
commas++;
if (intFound == 1) {
commaAfterInt = 1;
}
} else if (commas == 0) {
names[count][i] = dataPoint[i];
} else if (isdigit(dataPoint[i])) {
intFound = 1;
int j = 0;
for (j = 0; integerValue[j] != '\0'; j++);
integerValue[j] = dataPoint[i];
integerValue[j + 1] = '\0';
}
i++;
}
if (commas == 0) {
printf("Error: No comma in string.\n");
} else if (commas > 1) {
printf("Too many commas in input.\n");
} else if (commaAfterInt == 1) {
printf("Comma not followed by an integer\n");
} else {
point[count++] = atoi(integerValue);
}
}
printf("FORMATTED TABLE");
printf("%33s", title);
//printf("%23s", col1);
printf("%23s | %20s",col1,col2);
printf("\n -----------------------------------------------------\n");
int i = 0;
// printf("Count:%d\n",count);
while (i < count) {
printf("%23s | %20d", names[i], point[i]);
i++;
}
printf("FORMATTED HISTOGRAM");
i = 0;
while (i < count) {
printf("\n%28s ", names[i]);
int j = 0;
while (j < point[i]) {
printf("*");
j++;
}
printf("\n");
i++;
}
return 0;
}

Here are the expected answers and inputs to the program:

3: Compare outputA 0/4 Output differs. See highlights below. Special character legend Number of Novels Authored Author name Input Number of novels Jane Austen, 6 Enter a title for the data: You entered: Number of Novels Authored Enter the column 1 header: You entered: Author name Your output starts with Enter the column 2 header: You entered: Number of novels Enter a data point (-1 to stop input): Enter a data point (-1 to stop input) : Enter a title for the data: You entered: Number of Novels Authored Enter the column 1 header: You entered: Author name Expected output starts with Enter the column 2 header: You entered: Number of novels Enter a data point (1 to stop input): Data string: Jane Austen Data integer:6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <ctype.h>

int main()

{

char title[255];

printf("Enter a title for the data: ");

scanf("%[^\n]s", title);

printf("You entered: %s\n\n", title);

char col1[255], col2[255];

printf("Enter the column1 header: ");

scanf("\n%[^\n]s", col1);

printf("You entered: %s\n", col1);

printf("Enter the column2 header: ");

scanf("\n%[^\n]s", col2);

printf("You entered: %s\n\n", col2);

char data[255], data_str[100][255];

int data_int[100];

int i = 0;

// while (1)

// {

// printf("Enter a data point(-1 to stop input):");

// scanf("\n%[^\n]s", data);

// if (strcmp(data, "-1") == 0)

// {

// break;

// }

// char *d_str = strtok(data, ",");

// int d_int = atoi(strtok(NULL, ","));

// printf("Data string: %s\n", d_str);

// printf("Data Integer: %d\n", d_int);

// strcmp(data_str[i], d_str);

// data_int[i] = d_int;

// i++;

// }

i=0;

while (1)

{

printf("Enter a data point(-1 to stop input):");

scanf("\n%[^\n]s", data);

if (strcmp(data, "-1") == 0)

{

break;

}

int len = 0;

int cm = 0;

while (data[cm]!='\0')

{

if(data[cm]==',')

len++;

cm++;

}

char *d_str = strtok(data, ",");

char *token = strtok(NULL, ",");

int d_int = -999;

if(token!=NULL)

d_int = atoi(token);

if (len == 0)

printf("Error: No comma in string\n");

else if (len == 1)

{

if (d_int!=0)

{

printf("Data string: %s\n", d_str);

printf("Data Integer: %d\n", d_int);

strcpy(data_str[i], d_str);

data_int[i] = d_int;

i++;

}

else{

printf("Error: Comma not followed by an integer.\n");

}

}

else

{

printf("Error Too many commas in input.\n");

}

}

printf("%33s\n", title);

printf("%20s | %23s\n", col1, col2);

printf("-------------------------------------------------------------\n");

int j=0;

while(j<i){

printf("%20s | %23d\n", data_str[j], data_int[j]);

j++;

}

printf("\n\nHistogram:\n\n");

j=0;

while(j<i){

printf("%s ", data_str[j]);

int k=0;

while(k<data_int[j]){

printf("*");

k++;

}

printf("\n");

j++;

}

printf("\n");

return 0;

}

[rkv-sdc@loca1host 16θ32018]$ gcc -Ha11-9 visualization.c rkv-sdcQlocalhost 160320181$ ./a. out Enter a title for the data: N

Enter a data point (-1 to stop input) : Hary Shelley, 7 Data string: Hary Shelley Data Integer: Enter a data point (-1 to sto

Add a comment
Know the answer?
Add Answer to:
Please help modify my C program to be able to answer these questions, it seems 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
  • The program reads an unknown number of words – strings that all 20 characters or less...

    The program reads an unknown number of words – strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. ****** How do you I make it stop when control-d is entered. My code: #include <stdio.h> void main(void) { char sentence[100]; int i = 0; int count = 1; printf("Enter a...

  • Need help, i can't get a infinite loop to work, that terminates when a blank line...

    Need help, i can't get a infinite loop to work, that terminates when a blank line is inputted for the firstName. note: C programing langauge and using microsoft visual studios . #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h> #include <stdbool.h> int main() {    //int arraySalaries[50];    int calculate_avg = 0;    //int calculate_max;    //int calculate_min;    int salary[50];    //int length;    //int average_salary;    char firstName[50][50];    char lastName[50][50];    int i...

  • Can someone help me fix my C code. I am getting segmentation fault along with missing...

    Can someone help me fix my C code. I am getting segmentation fault along with missing termination " /* These are the included libraries. */ #include #include #include #include void printTriangle(char *str); int main() { char sentence[81]; int done = 0; while(done != 1) { printf("Please enter the sentence or quit: "); fgets(sentence, 80, stdin); if(strcmp(sentence, "quit") == 0) done = 1; else printTriangle(sentence); } } void printTriangle(char *str) { int index = 0; int line = 1; int i;...

  • Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...

    Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program A3a without modification: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]); void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]); void getStudents(int COLS, char students[COLS][20]); void printStudents(int COLS, char students[COLS][20]); void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]); void printFinalGrades(int COLS, char Fgrades[]); int main() {    srand(time(0));    int stu = 0, assign = 0;...

  • Program: Data visualization

    5.10 LAB*: Program: Data visualization(1) Prompt the user for a title for data. Output the title. (1 pt)Ex:Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)Ex:Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • Write helpful comments for the following code: use Vigenere cipher tech to encrypt and decrypt message...

    Write helpful comments for the following code: use Vigenere cipher tech to encrypt and decrypt message The code: #include<stdio.h> #include <stdlib.h> char arr[26][26]; char message[22], key[22], emessage[22], retMessage[22]; int findRow(char); int findColumn(char); int findDecRow(char, int); int main() { int i = 0, j, k, r, c; k = 96; for (i = 0; i<26; i++) { k++; for (j = 0; j<26; j++) { arr[i][j] = k++; if (k == 123) k = 97; } } printf("\nEnter message\n"); fgets(message, 22,...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

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