Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1) Prompt the user for a title for data. Output the title. ex. Enter a title fo

ID: 3731036 • Letter: #

Question

(1) Prompt the user for a title for data. Output the title.

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.

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 have finished entering data points. Output the data points. Store the string components of the data points in an array of strings. Store the integer components of the data points in an array of integers.

ex.

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

Jane Austen, 6

Data string: Jane Austen

Data integer: 6

(4) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message and prompt again for a valid data point.

ex.

Enter a data point (-1 to stop input): Ernest Hemingway 9 Error: No comma in string.

Enter a data point (-1 to stop input): Ernest, Hemingway, 9 Error: Too many commas in input.

(5) Output the information in a formatted table. The title is right justified with a width of 33. Column 1 has a width of 20. Column 2 has a width of 23.

ex.

Number of Novels Authored

Author name | Number of novels

--------------------------------------------

Jane Austen | 6

Charles Dickens | 20

Ernest Hemingway | 9

Jack Kerouac | 22


(6) Output the information as a formatted histogram. Each name is right justified with a width of 20

ex.

Jane Austen ******

Charles Dickens ********************

Ernest Hemingway *********

Jack Kerouac **********************

Here is what I have so far, the spacing isn't coming out right, please help me fix this!!!

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

Explanation / Answer

#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: ");
fgets (title, 50, stdin);
printf("You entered: %s ", title);
printf("Enter the column 1 header: ");
fgets (col1, 50, stdin);
printf("You entered: %s ", col1);
printf("Enter the column 2 header: ");
fgets (col2, 50, stdin);
printf("You entered: %s ", col2);
col1[strlen(col1) - 1] = '';
col2[strlen(col2) - 1] = '';
int count = 0;
char dataPoint[50];
while (count < 50) {
printf("Enter a data point(-1 to stop input): ");
fgets (dataPoint, 50, stdin);
if (atoi(dataPoint) == -1) {
break;
}
int commas = 0;
int i = 0;
int intFound = 0;
char integerValue[5] = "";
int commaAfterInt = 0;
  
while (dataPoint[i] != '') {
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] != ''; j++);
integerValue[j] = dataPoint[i];
integerValue[j + 1] = '';
}
i++;
}
if (commas == 0) {
printf("Error: No comma in string. ");
} else if (commas > 1) {
printf("Too many commas in input. ");
} else if (commaAfterInt == 1) {
printf("Comma not followed by an integer ");
} else {
point[count++] = atoi(integerValue);
}
}
printf("*****FORMATTED TABLE***** ");
printf("%33s", title);
//printf("%23s", col1);
printf("%20s | %23s",col1,col2);
printf(" ----------------------------------------------------- ");
int i = 0;
// printf("Count:%d ",count);
while (i < count) {
// if yo want to use left justify in table data use - after % like
// printf("%-20s | %-23d", names[i], point[i]);
int l1 = strlen(names[i]);//.length();
printf("%s | %d", names[i], point[i]);
printf(" ");
i++;
}
printf("FORMATTED HISTOGRAM");
i = 0;
while (i < count) {
printf(" %s ", names[i]);
int j = 0;
while (j < point[i]) {
printf("*");
j++;
}
printf(" ");
i++;
}
return 0;
}

//Here is the output

You entered: Auther name   
  
Enter the column 2 header:   
Number of Novels   
You entered: Number of Novels
  
Enter a data point(-1 to stop input):
Jane Austen,6
Enter a data point(-1 to stop input):
Charles Dickens,20   
Enter a data point(-1 to stop input):
F. Scott Fitzgeraid,8
Enter a data point(-1 to stop input):
-1   
*****FORMATTED TABLE*****
Number of Novel Authered   
Auther name | Number of Novels   
-----------------------------------------------------   
Jane Austen | 6
Charles Dickens | 20   
F. Scott Fitzgeraid | 8
FORMATTED HISTOGRAM
Jane Austen ******   
  
Charles Dickens ********************   
  
F. Scott Fitzgeraid ********