2014-03-31 33 views
-2

好了,我正在写一个程序,使0 & 9之间的10×10阵列填充有随机数,并且(具有组织成一个函数的每个步骤):程序用C不工作

(一)打印出第一行并打印出来 (b)打印出主对角线的平均值(从上到下,从左到右) (c)打印出第一列有多少个0 (d)多出10 x 10个数组,其中0,&9之间的随机数,并且如果主对角线(从上到下,从左到右)中的所有值均为7或更大,则打印数组,并输出 的尝试次数。如果它不能在1,000,000次尝试中完成,则打印它不能完成 。 (e)将含有-10 &之间+10,乘10号的一维动态分配的数组由 第一阵列制成,显示结果矢量

想不通有什么不使其工作,得到错误的所有当他们打印出来的步骤值:'(和一些错误

void simple_array(int ten_by_ten[10][10]) 
{ 
    int i, j; 
    printf("\n"); 
    for (i=0; i<10; ++i)  
    { 
     for (j=0; j<10; ++j) 
     { 
      ten_by_ten[i][j] = rand() % 10; 
      printf("%d ", ten_by_ten[i][j]); 
     } 
     printf("\n"); 
    } 
} 
void sum_first_row(int y[10][10]) 
{ 
    int i = 0, j, sum_row = 0; 
    for (j=0; j<10; ++j) 
    { 
     sum_row += y[i][j]; 
    } 
    printf("\nThe sum of the first row is: %d\n", sum_row); 
} 

void average_main_diagonal(int z[10][10]) 
{ 
    int i, j = 0, average_diagonal = 0; 
    for (i=0; i<10; ++i) 
    { 
     ++j; 
     average_diagonal += z[i][j]; 
    } 
    printf("\nThe average of the diagonal is: %lf\n", (average_diagonal/10.0)); 
} 

void zeros(int a[10][10]) 
{ 
    int i, j = 0, zeroz = 0; 
    for (i=0; i<10; ++i) 
    { 
     if (a[i][j] == 0) 
      ++zeroz; 
    } 
    printf("\nThere are %d zero's in the first column\n", zeroz); 
} 

void multiple_arrays() 
{ 
    int sum_diagonal = 0,array[10][10], i, j, k, l, c; 
    while ((sum_diagonal < 70) && (c <= 1000000)) 
    { 
     j = 0; 
     k = 0; 
     l = 0; 
     i = 0; 
     for (i=0; i<10; ++i) 
     { 
      for (j=0; j<10; ++j) 
      { 
       array[i][j] = rand() % 10; 
      } 
     } 
     for (k=0; k<10; ++k) 
     { 
      ++l; 
      sum_diagonal += array[k][l]; 
     } 
     ++c; 
    } 
    if (c = 1000000) 
     printf("\nCould not get a diagonal with numbers >= 7\n"); 
    else 
    { 
     j = 0; 
     i = 0; 
     for (i=0; i<10; ++i); 
     { 
      printf("\n"); 
      for (j=0; j<10; ++j) 
       printf("%d ", array[i][j]); 
     } 
     printf("It took %d many tries to get a diagonal with all numbers >= 7", c); 
    } 

} 

void array_multiplication(int b) 
{ 
    int **arrays, i, j, k, l, m, prod[10]; 
    arrays = (int **) calloc (10, sizeof(int *)); 
    for (i=0; i<10; ++i) 
     arrays[i] = (int *) calloc (1, sizeof(int)); 
    for (i=0; i<10; i=i+1) 
    { 
     arrays[i] = (rand() % 21) -10;  
    } 

    for (k=0; k<10; ++k) 
    { 
     prod[k] = 0; 
     for (l=0; l<10; ++l) 
      prod[k] = prod[k] + b[k][l] * arrays[l]; 
    } 
    printf ("The product is: <"); 
    for (m=0; m<10; ++m) 
     printf ("%d, ", prod[m]); 
    printf (">\n"); 
} 

int main() 
{ 
    int x[10][10]; 

    simple_array(x); 
    sum_first_row(x) 
    average_main_diagonal(x); 
    zeros(x); 
    multiple_arrays(); 
    array_multiplication(x); 


    return (0); 
} 

收到以下错误:

当我注释掉‘阵乘法’功能(因为它得到了以下错误:(a)赋值使得来自int的指针eger = array [i] =(rand()%21)-10;“(b)value既不是数组也不是指针”prod [k] = prod [k] + b [k] [l] * arrays [ 1];”(c)使的ARG1‘array_multiplication’使得整数指针,未转换‘array_multiplication(X);’

并将其打印出对角线

帮助的一个不正确的平均是极其理解!! ! 谢谢, - 罗布

+1

为什么不等待40分钟前发布的[上一个问题](http://stackoverflow.com/questions/22752622/c-program-not-working-properly),而不是创建另一个问题?如果你需要,编辑并重写那个,而不是创建一个新的。 – legends2k

回答

0

arrays[i] = (rand() % 21) -10;arrays[i]是一个指针。您不能将整数指定给指针。实际上,你只是在上一行中为arrays[i]分配了一些分配的内存,所以即使这样做你也会泄漏该内存。

也许你打算有两个嵌套循环,如果你想在每一行和每一列放置一个值? 你也永远不会释放你的记忆calloc'd。并且不要强制转换calloc返回的值。

其他错误,你的意思是你的声明功能

void array_multiplication(int b[10][10]) 

,而不是(int b)。它抱怨你正在做一个int的阵列操作。

+0

谢谢!我要去试试 – Snorklebort

+0

如何修复“数组[i] =(rand()%21)-10;” &calloc问题?我基本上应该动态分配一维数组,然后乘以第一个数组。但我不确定如何正确动态分配内存。 谢谢:) – Snorklebort

+0

你分配的内存OK。你需要使用两个索引来访问'int's,因为它是一个二维数组,虽然,例如'arrays [i] [j] = rand()...' –

0

你的代码看起来对角线是不正确的(你知道的)。在使用它之前你正在增加“j”......所以“i”是0,“j”是1 ......这不是对角线。你或者需要在使用它来查找数值之后做“++ j”,但是更适合使用z [i] [i](对于BOTH指数使用“i”)。

这个问题发生在average_main_diagonal和multiple_arrays。

+0

太棒了!这固定了对角线的平均问题,谢谢! – Snorklebort