gpt4 book ai didi

c - 我如何从二维数组中获取每列的总和并将其保存到 C 中的一维数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:31 28 4
gpt4 key购买 nike

在第一个函数中,我用 0(表示用户不是 friend ,或者 1 表示他们是 friend )填充 5x5 数组。第二个功能计算每个用户的 friend 。第三个功能计算并返回用户之间的共同好友。我对最后一个函数有问题,它必须计算每个用户的 friend 并将其保存在一维数组中,然后将数组冒泡。

谁能帮我找出最后一个函数有什么问题

友情数组是

01100
10111
11010
01100
01000

代码

#include <stdio.h>
#include <stdlib.h>

/* function declaration */
void loadMatrix (int **F, int size);
int findFriends (int **F, int size, int user);
int commonFriends (int **F, int size, int user1, int user2);
void sortUsers (int **F, int size, int *S);

int main()
{
int **matrixF; /* friendship array */
int *matrixS; /* sum of each user array */
int num_users; /* users number */
int i, j;

printf("Give number of users: ");
scanf("%d", &num_users);

/* allocate memory for array */
matrixF = (int **) malloc(num_users * sizeof(int *));
if (!matrixF)
{
printf("Memory allocation error!\n");
exit(1);
}

for (i = 0; i < num_users; ++i)
{
matrixF[i] = (int *) malloc(num_users * sizeof(int));
if (!matrixF[i])
{
printf("Memory allocation error!\n");
exit(1);
}
}

loadMatrix(matrixF, num_users);


for (i=0; i<num_users; ++i)
{
printf("Number of friends of user %d: %d\n", i, findFriends(matrixF, num_users, i));
}

/* common friends */
for (i=0; i<num_users; ++i)
{
for (j=0; j<i; ++j)
{
printf("Number of common friends of %d and %d: %d\n", i, j, commonFriends(matrixF, num_users, i, j));
}
}

matrixS = (int *)malloc(num_users * sizeof(int));
if (!matrixS)
{
printf("Memory allocation error!\n");
exit(1);
}

sortUsers(matrixF, num_users, matrixS);

/* bubble short */
for (i=0; i<num_users; ++i)
{
printf("%d friends.\n", matrixS[i]);
}

return 0;
}

void loadMatrix (int **F, int size)
{
int i, j;

for (i=0; i<size; i++)
for (j=0; j<size; j++)
{
do
{
printf(" user%d is friend with user%d: ", i, j);
scanf("%d", &F[i][j]);
if ((F[i][j]<0) || (F[i][j]>1 )) printf("H timi prepei einai 0 i 1 . ");
}
while ((F[i][j]<0) || (F[i][j]>1)); /*please enter 0 or 1*/
}
}


int findFriends (int **F, int size, int user) {

int j;
int sum=0;

for(j=0; j<size; j++)
{
sum+=F[user][j];
}
return (sum);
}


int commonFriends (int **F, int size, int user1, int user2) {

int j;
int counter=0;

for (j=0; j<size; j++)
{
if ((F[user1][j]==1)&&(F[user2][j]==1))
counter++;
}
return (counter);
system("pause");
}

void sortUsers (int **F, int size, int *S)
{
int i, j, temp;
int sum=0;

for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
sum+=F[i][j];
}
S[i]=sum;

printf("%d\t", S[i]);
for(i=0; i<size; i++)
{
S[i]=sum;
}

for (i=1; i<size; i++)
{
for (j=0; j<size-1; j++)
{
if (S[j]>S[j+1])
{
temp = S[j];
S[j] = S[j+1];
S[j+1] = temp;
}
}
}
}

最佳答案

我刚刚找到答案

void sortUsers (int **F, int size, int *S) {

int i, j, temp, user;
int sum;

for (i=0; i<size; i++){
sum=0;
for (j=0; j<size; j++)
S[i]=(sum+=F[i][j]);

}

for (i=0; i<size; i++)
printf("%d\t", S[i]);






for (i=1; i<size; i++) {
for (j=0; j<size-1; j++) {
if (S[j]>S[j+1]) {
temp = S[j];
S[j] = S[j+1];
S[j+1] = temp;
}
}
}



system("pause");


}

关于c - 我如何从二维数组中获取每列的总和并将其保存到 C 中的一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30133485/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com