我是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 %in", 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: %dn", 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: %dnn", totalsales);
printf("nThe total sales for product 1 for the past ten days is: %d", totproducttenone);
}//closes printarr function
这期
1)您已经将所有变量声明为双精度,并使用'%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
因为数组中没有对应位置的值,所以可能需要修改