gpt4 book ai didi

c - 在c中交换二维数组的行

转载 作者:行者123 更新时间:2023-11-30 20:37:00 26 4
gpt4 key购买 nike

目前我想知道如何交换二维数组中已有的值,在本例中我设法交换它们,但是,我似乎无法将它们交换回来......如何我可以让程序来回交换数组的值吗?

代码:

#include <stdio.h>

void main()
{
//declaration of values
int d = 0;
char value;
float list[5][6], swaplist[5][6];
float average_odd, sum = 0, average_even, sum1 = 0;

//Command list so that the user knows what he can do in this program
printf("Command list:\t \n\nCommand: \t\t Output: ");
printf("\n \"A\" \t\t Declare values of a list.\n \"O\" \t\t Obtain the average value of the even and odd column\n\t\t values in the list.\n");
printf(" \"I\" \t\t Exchange the values on the even columns with the odd ones.\n \"P\" \t\t Print the values of the list.\n \"S\" \t\t End program.");
//========================================================================================================
while (d != 1)
{
printf("\n\nInsert value: ");
scanf(" %c", &value);
//=========================================================================================================
if (value == 'a' || value == 'A')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
printf("Insert value of element %d in column %d: ", i + 1, j + 1);
scanf("%f", &list[i][j]);
swaplist [i][j] = list[i][j];
}
}
}
//=========================================================================================================
if (value == 's' || value == 'S')
{
d++;
}
//=========================================================================================================
if (value == 'P' || value == 'p')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
printf("%2.2f ", list[i][j]);
}
printf("\n");
}
}
//========================================================================================================
if (value == 'o' || value == 'O')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j = j + 2)
{
sum += list[i][j];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 1; j < 6; j = j + 2)
{
sum1 += list[i][j];
}
}
average_even = sum1 / 15;
printf("The average of the even columns = %.2f\n", average_even);
average_odd = sum / 15;
printf("The average of the odd columns = %.2f\n", average_odd);
}
//=======================================================================================================
if (value == 'i' || value == 'I')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j = j + 2)
{
list[i][j] = swaplist[i][j+1];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 1; j < 6; j = j + 2)
{
list[i][j] = swaplist[i][j - 1];
}
}
}
}
}

最佳答案

交换后,您不会更改交换列表的状态。首先,交换列表是未交换矩阵的副本。您不会更改它来反射(reflect)矩阵中的更改,也不会跟踪任何其他状态。

您实际上并不需要交换列表。我建议您定期交换相邻列的单元格:

    if (value == 'i' || value == 'I') {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j = j + 2) {
float swap = list[i][j];

list[i][j] = list[i][j + 1];
list[i][j + 1] = swap;
}
}
}

这将在重复交换时切换矩阵。

关于c - 在c中交换二维数组的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34021506/

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