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

9.7 Project 4: Data visualization (C Language) This is all one problem, supposed

ID: 3724732 • Letter: 9

Question

9.7 Project 4: Data visualization (C Language)

This is all one problem, supposed to be written in one program.

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

Ex:


(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)

Ex:


(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. (4 pts)

Ex:


(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.

If entry has no comma

Output: Error: No comma in string. (1 pt)

If entry has more than one comma

Output: Error: Too many commas in input. (1 pt)

If entry after the comma is not an integer

Output: Error: Comma not followed by an integer. (2 pts)


Ex:


(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. (3 pts)

Ex:


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

Ex:

Explanation / Answer

#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("%[^ ]s", title);

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

int col1[255], col2[255];

printf("Enter the column1 header: ");

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

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

printf("Enter the column2 header: ");

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

printf("You entered: %s ", 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(" %[^ ]s", data);

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

// {

// break;

// }

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

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

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

// printf("Data Integer: %d ", 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(" %[^ ]s", data);

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

{

break;

}

int len = 0;

int cm = 0;

while (data[cm]!='')

{

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 ");

else if (len == 1)

{

if (d_int!=0)

{

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

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

strcpy(data_str[i], d_str);

data_int[i] = d_int;

i++;

}

else{

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

}

}

else

{

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

}

}

printf("%33s ", title);

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

printf("------------------------------------------------------------- ");

int j=0;

while(j<i){

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

j++;

}

printf(" Histogram: ");

j=0;

while(j<i){

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

int k=0;

while(k<data_int[j]){

printf("*");

k++;

}

printf(" ");

j++;

}

printf(" ");

}