Note that there are some explanatory texts on larger screens.

plurals
  1. PO2D array is truncating some characters
    text
    copied!<p>I just need to know how to fix this.</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #define PRICE 1985.95 #define MAX_LENGTH 20 #define MAX_EMPLOYEES 25 #define MAX_WEEKS 7 void getNames(int* employeeNumber, char names[][MAX_LENGTH]); void getSales(int* employeeNumber,int* weeksNumber, int weeks[][MAX_WEEKS+1]); void calcEachTotal(int empNumber, int weekNumber, int weeks[][MAX_WEEKS+1]); void calcTotal(int,int,int*,int [][MAX_WEEKS+1],int [][3]); void calcCoTot(int ,int*, int [][3]); void outputDataScreen(int,int,int,char [][MAX_LENGTH],int [][MAX_WEEKS+1], int [][3]); void outputDataFile(int,int,int,char [][MAX_LENGTH],int [][MAX_WEEKS+1], int [][3]); int main (void) { char employees[MAX_EMPLOYEES][MAX_LENGTH] = {{0}}; //holds personal sales info and each employees total sales int sales[MAX_EMPLOYEES][MAX_WEEKS+1]={{0}}; //holds the weekly lowest,highest and total int weeklyStats[MAX_WEEKS][3] = {{0}}; int employeeNumber; int weeksNumber; int companyTotal = 0; getNames(&amp;employeeNumber,employees); getSales(&amp;employeeNumber, &amp;weeksNumber,sales); calcEachTotal(employeeNumber, weeksNumber, sales); //call function to calculate personal,weekly and company total sales calcTotal(employeeNumber,weeksNumber,&amp;companyTotal,sales,weeklyStats); calcCoTot(weeksNumber,&amp;companyTotal, weeklyStats); //call two functions to output the data to the screen and the output text file outputDataScreen(employeeNumber,weeksNumber,companyTotal,employees,sales,weeklyStats); outputDataFile(employeeNumber,weeksNumber,companyTotal,employees,sales,weeklyStats); return 1; } /*===============getNames====================== Pre : NAMES.txt is an open file. Post : Number of employees This function will read the number of emplyees and close if there is more than 25 employees */ void getNames(int* employeeNumber, char names[][MAX_LENGTH]) { FILE* spNames; int i; int j; // statements spNames = fopen("/Users/r3spectak/Desktop/NAMES.TXT", "r"); if (!spNames) { printf("Could not open NAMES.txt\n\n"); exit(100); } fscanf(spNames,"%d",employeeNumber); if (*employeeNumber &gt; MAX_EMPLOYEES ) { printf("Unable to read more than 25 employees\n\n"); exit(100); } for(i = 0; i&lt; *employeeNumber;i++) for(j = 0; j&lt;= MAX_LENGTH; j++) fscanf(spNames,"%c", &amp;names[i][j]); fclose(spNames); } /*=====getSales============== Pre : SALES.txt is an open file. number of week and employee number with the array of weeks. Post : number of weeks This function reads the number of weaks and close */ void getSales(int* employeeNumber,int* weeksNumber, int weeks[][MAX_WEEKS+1]) { // Local variables FILE* spSales; int i; int j; // Statements spSales = fopen("/Users/r3spectak/Desktop/SALES.TXT", "r"); if (!spSales) { printf("Could not open Sales.txt\n\n"); exit(102); } fscanf(spSales,"%d",weeksNumber); if (*weeksNumber &gt; MAX_WEEKS) { printf("Unable to read more than 6 weeks\n\n"); exit(102); } for(i = 0 ; i &lt; *employeeNumber;i++) for(j = 0 ; j &lt; *weeksNumber ; j++) fscanf(spSales,"%d", &amp;weeks[i][j]); fclose(spSales); } /*===========calcEachTotal==================== Pre : Number of employees, Number of weeks Post : Employee total This function calculates the weekly total for each employee and store in an array */ void calcEachTotal(int employeeNumber, int weekNumber, int weeks[][MAX_WEEKS+1]) { // Local Variables int i; int j; // Statements for(i = 0; i&lt; employeeNumber; i++) for(j=0, weeks[i][MAX_WEEKS] = 0; j&lt; weekNumber;j++) weeks[i][MAX_WEEKS] += weeks[i][j]; } /*===========calcTotal==================== Pre : the number of the employees and the weeks , two arrays that hold the names and sales info and Stats array which is for weekly low,high and total Post : calculates the weekly total for each employee. the highest, lowest and the total of each week and the company total and stores them in the arrays This function */ void calcTotal(int employeeNumber, int weekNumber,int *companyTotal, int weeks[][MAX_WEEKS+1],int stats[][3]) { // Local Variables int i; int j; // Statements //calculate the weekly highest, lowest and total and store it in the stats array for (i = 0 ; i&lt;weekNumber ; i++) { stats[i][0]=weeks[0][i]; stats[i][1]=weeks[0][i]; stats[i][2]=0; for (j=0; j &lt;employeeNumber; j++) { if (weeks[j][i] &gt; stats[i][0]) stats[i][0] = weeks[j][i]; if (weeks[j][i] &lt; stats[i][1]) stats[i][1] = weeks[j][i]; stats[i][2] += weeks[i][j]; } } } /*===========calcCoTot==================== Pre : the number of the employees and the weeks , two arrays that hold the names and sales info and Stats array which is for weekly low,high and total Post : calculates the weekly total for each employee. the highest, lowest and the total of each week and the company total and stores them in the arrays This function */ void calcCoTot(int weekNumber,int *companyTotal, int stats[][3]) { // Local Variables int i; int j; //calculate the company total for( i = 0; i &lt; weekNumber ; i++) *companyTotal += stats[i][2]; } //===============outputDataScreen======== /* pre : the number of the workers and weeks, a char array that holds the names , an int array for personal sales and another int array stats which holds weekly info , also another int for company total post : outputs all the information in a table to the screen */ void outputDataScreen(int empNum,int weeks,int companyTotal,char names[][MAX_LENGTH],int sales[][MAX_WEEKS+1], int stats[][3]) { // Local Variables int i; int j; int k; // Statements printf("\t\tHomework 2: Two Dimensional Arrays\n"); printf(" *** Sales Table *** \n\n"); printf("=============\t\t"); for ( i = 0; i &lt; weeks; i++) printf("======\t"); printf("\nSalesperson\t\t"); for ( i = 0; i&lt;weeks; i++) printf("Week%d\t",i+1); printf("\n=============\t\t"); for ( i = 0; i &lt; weeks; i++) printf("======\t"); for ( i = 0 ; i &lt; empNum ; i++) { for ( j = 0 ; j&lt;= MAX_LENGTH; j++) printf("%c",names[i][j]); //printf(""); for(k = 0; k &lt; weeks;k++) printf("\t%d",(int)sales[i][k]); } printf("\n*** The output is in OUTPUT.TXT ***\n\t\t\tEnd of the program!\n\t\t\tHave a great day!\n"); } //===============outputDataFile======= /* pre : the number of the workers and weeks, a char array that holds the names , an int array for personal sales and another int array stats which holds weekly info , also another int for company total post : outputs all the information in a table to the File */ void outputDataFile(int empNum,int weeks,int companyTotal,char names[][MAX_LENGTH],int sales[][MAX_WEEKS+1], int stats[][3]) { // Local Variables FILE* fpOut; int i; int j; int k; //open the txt file to output the information // Statements fpOut = fopen("/Users/r3spectak/Desktop/OUTPUT.TXT","w"); fprintf(fpOut,"\t\tHomework 2: Two Dimensional Arrays\n"); fprintf(fpOut," *** Sales Table *** \n\n"); fprintf(fpOut,"Number of Employees: %d\n",empNum); fprintf(fpOut,"Number of Weeks: %d\n\n",weeks); fprintf(fpOut,"Personal sales info\n"); fprintf(fpOut,"=============\t\t"); for ( i = 0; i &lt; weeks; i++) fprintf(fpOut,"======\t"); fprintf(fpOut,"\nSalesperson\t\t"); for ( i = 0; i&lt;weeks; i++) fprintf(fpOut,"Week%d\t",i+1); fprintf(fpOut,"\n=============\t\t"); for ( i = 0; i &lt; weeks; i++) fprintf(fpOut,"======\t"); for ( i = 0 ; i &lt; empNum ; i++) { for ( j = 0 ; j &lt; MAX_LENGTH ; j++) fprintf(fpOut,"%c",names[i][j]); fprintf(fpOut," "); for(k = 0 ; k &lt; weeks;k++) fprintf(fpOut,"%-.2f ",(double)sales[i][k]*PRICE); fprintf(fpOut,"%-.2f ",(double)sales[i][MAX_WEEKS]*PRICE); fprintf(fpOut,"*\n"); } fprintf(fpOut,"\n\nWEEKLY INFO :\n\n"); fprintf(fpOut,"HIGHEST SALE "); for (i=0 ; i &lt; weeks ; i++ ) fprintf(fpOut,"%-.2f ",(double)stats[i][0]*PRICE); fprintf(fpOut,"\n"); fprintf(fpOut,"LOWEST SALE "); for (i=0 ; i &lt; weeks ; i++ ) fprintf(fpOut,"%-.2f ",(double)stats[i][1]*PRICE); fprintf(fpOut,"\n"); fprintf(fpOut,"WEEKLY TOTAL "); for (i=0 ; i &lt; weeks ; i++ ) fprintf(fpOut,"%-.2f ",(double)stats[i][2]*PRICE); fprintf(fpOut,"\nCOMPANY TOTAL SALES : %-.2lf\n\n",(double)companyTotal*PRICE); fclose(fpOut); } </code></pre> <p>NAMES.txt</p> <pre><code> 16 Kelly, Victor Lam, Gary Nagasake, David Nguyen, Jeff Nguyen, Michael Sinn, Scott Smith, Jacob Son, Thai Tavares, Maribel Tran, Diane Tsukamoto, Andrew Wang, Mary Young, Daniel Wells, Steve Wong, Justin Johnson, Mary </code></pre> <p>SALES.txt</p> <pre><code> 4 25 20 25 25 20 22 23 22 25 26 25 22 30 28 25 26 25 30 45 20 30 25 20 21 27 25 24 26 20 23 24 20 28 26 24 25 30 10 35 32 28 29 30 35 15 16 15 14 12 15 12 19 20 24 20 18 10 15 12 16 32 30 33 29 </code></pre> <p>The program works and all but im getting a problem with the array. it is not printing it right. the order of the characters is running over the place and some names are truncated. Is there something wrong with the declaration of the arrays? or do i have to print white space? someone please help me!</p> <p>OUTPUT.txt</p> <pre><code> *** Sales Table *** Number of Employees: 16 Number of Weeks: 4 Personal sales info ============= ====== ====== ====== ====== Salesperson Week1 Week2 Week3 Week4 ============= ====== ====== ====== ====== Kelly, Victor 49648.75 39719.00 49648.75 49648.75 188665.25 * Lam, Gary 39719.00 43690.90 45676.85 43690.90 172777.65 * Nagasake, David 49648.75 51634.70 49648.75 43690.90 194623.10 * Nguyen, Jeff 59578.50 55606.60 49648.75 51634.70 216468.55 * Nguyen, Michae 49648.75 59578.50 89367.75 39719.00 238314.00 * Sinn, Scott 59578.50 49648.75 39719.00 41704.95 190651.20 * Smith, Jacob 53620.65 49648.75 47662.80 51634.70 202566.90 * Son, Thai 39719.00 45676.85 47662.80 39719.00 172777.65 * Tavares, M 55606.60 51634.70 47662.80 49648.75 204552.85 * ribel Tran, Dia 59578.50 19859.50 69508.25 63550.40 212496.65 * e Tsukamot 55606.60 57592.55 59578.50 69508.25 242285.90 * , Andrew Wang, M 29789.25 31775.20 29789.25 27803.30 119157.00 * ry Young, 23831.40 29789.25 23831.40 37733.05 115185.10 * Daniel Wells 39719.00 47662.80 39719.00 35747.10 162847.90 * Steve Wong 19859.50 29789.25 23831.40 31775.20 105255.35 * Justin Joh 63550.40 59578.50 65536.35 57592.55 246257.80 * WEEKLY INFO : HIGHEST SALE 63550.40 59578.50 89367.75 69508.25 LOWEST SALE 19859.50 19859.50 23831.40 27803.30 WEEKLY TOTAL 722885.80 734801.50 822183.30 909565.10 COMPANY TOTAL SALES : 3189435.70 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload