我是C编程的新手,似乎无法让我的2d数组正常运行。我知道我没有太多数据要输入,但所有剩余的数组分配应该为零。当我编译并运行它。我得到了一些巨大的数字,我无法理解什么是错的。
/*************** PREPROCESSOR DIRECTIVES *****************/
#include<stdio.h>
void printArr( double a[5][4] );
/***************** MAIN FUNCTION *********************/
int main(void)
{
/************ DECLARATION OF VARIABLES *****************/
int PRODUCT = 5;
int SALESPERSON = 4;
int DAY = 10;
int x, y , z;
/********** THE EXECUTABLE PART OF THE PROGRAM ***********/
//initialize array for product, salesperson, and day
double arr[5][4] = {1,2,3,4,5,6,7,8,9,9,8,7,6,5};
printArr(arr);
}// closes main method
void printArr( double arr[5][4] ){
/********** Declaration of Variables **************/
double value;
int PRODUCT = 5;
int SALESPERSON = 4;
int x,y,z,i,j;
int eleven = 11;
double productone, producttwo,productthree,productfour,productfive = 0;
double salesone, salestwo, salesthree, salesfour = 0;
double totalproduct, totalperson, totalsales = 0;
double totproducttenone, totproducttentwo, totproducttenthree = 0;
double totproducttenfour, totproducttenfive = 0;
double totsalestenone, totsalestentwo, totsalestenthree = 0;
double totsalestenfour= 0;
//for loop for the days
for(x = 1; x < eleven; x++){
productone = 0;
producttwo = 0;
productthree = 0;
productfour = 0;
productfive = 0;
salesone = 0;
salestwo = 0;
salesthree = 0;
//prints the day
printf("\n Day %i\n", x);
//for loop for the rows
for(y = 0; y < PRODUCT; y++){
//for loop for the columns
for(z = 0; z < SALESPERSON; z++){
i = 0;
i = y + 1;
j = 0;
j = z + 1;
printf("\n---------------------------------");
printf("\n|Product: %i | Salesperson: %i |\n", i, j);
printf(" sales total = %d", arr[y][z]);
//adds up product 1
if(y == 0){
productone = productone + arr[y][z];
}//closes if for product 1
//adds up product 2
if(y == 1){
producttwo = producttwo + arr[y][z];
}//closes if for product 2
//adds up product 3
if(y == 2){
productthree = productthree + arr[y][z];
}// closes if for product 3
//adds up product 4
if(y ==3){
productfour = productfour + arr[y][z];
}//closes if for product 4
//adds up product 5
if(y == 4){
productfive = productfive + arr[y][z];
}//closes if for product 5
//adds up salesperson 1
if(z == 0){
salesone = salesone + arr[y][z];
}//closes if for salesperson one
//adds up salesperson 2
if(z == 1){
salestwo = salestwo + arr[y][z];
}//closes if for salesperson 2
//adds up salesperson 3
if(z == 2){
salesthree = salesthree + arr[y][z];
}//closes if for sales person 3
//adds up salesperson 4
if(z == 3){
salesfour = salesfour + arr[y][z];
}// closes if for salesperson 4
//if for totalsales
if(arr[y][z] != 0){
totalsales = totalsales + arr[y][z];
}// closes if for totalsales
//adds up the total amount sold for products
totalproduct = productone + producttwo + productthree + productfour + productfive;
//adds up the totale amoun sold for salesperson
totalperson = salesone + salestwo + salesthree + salesfour;
}//closes loop for columns
}//closes loop for rows
//prints the sales figures for the day
printf("\nHere are the sales figures for each product,");
printf("\nsales person, total amount sold for today:\n");
printf("\nTotal sales for product 1 is: %d", productone);
printf("\nTotal sales for product 2 is: %d", producttwo);
printf("\nTotal sales for product 3 is: %d", productthree);
printf("\nTotal sales for product 4 is: %d", productfour);
printf("\nTotal sales for product 5 is: %d", productfive);
printf("\nTotal sales for salesperson 1 is: %d", salesone);
printf("\nTotal sales for salesperson 2 is: %d", salestwo);
printf("\nTotal sales for salesperson 3 is: %d", salesthree);
printf("\nTotal sales for salesperson 4 is: %d", salesfour);
printf("\nThe totalsales of products sold today is: %d\n", totalproduct);
totproducttenone = totproducttenone + productone;
totproducttentwo = totproducttentwo + producttwo;
totproducttenthree = totproducttenthree + productthree;
totproducttenfour = totproducttenfour + productfour;
totproducttenfive = totproducttenfive + productfive;
totsalestenone = totsalestenone + salesone;
totsalestentwo = totsalestentwo + salestwo;
totsalestenthree = totsalestenthree + salesthree;
totsalestenfour = totsalestenfour + salesfour;
}//closes loop for days
//prints the sales for the past ten days
printf("\nThe total of sales over the past ten days is: %d\n\n", totalsales);
printf("\nThe total sales for product 1 for the past ten days is: %d", totproducttenone);
}//closes printarr function
答案 0 :(得分:1)
问题的
1)您已将所有变量声明为double并使用'%d'作为int数据类型的格式说明符来打印值,因此将所有%d更改为%f (小心;仅用于printf语句中声明为double的元素)
2)我不知道这是一个功能,还是你打算以这种方式使用该程序。
数组
double arr[5][4] = {1,2,3,4,5,6,7,8,9,9,8,7,6,5};
必须包含20个元素,但只有14个元素,因此剩下的6个元素的值被视为0.这会导致错误,
印刷
---------------------------------
|Product: 4 | Salesperson: 4 |
sales total = 0.000000
---------------------------------
|Product: 5 | Salesperson: 1 |
sales total = 0.000000
---------------------------------
|Product: 5 | Salesperson: 2 |
sales total = 0.000000
---------------------------------
|Product: 5 | Salesperson: 3 |
sales total = 0.000000
---------------------------------
|Product: 5 | Salesperson: 4 |
sales total = 0.000000
因为数组没有该位置的任何值,所以可能想要改变它。