меня и моего друга пытаются передать определяемый пользователем массив функции и выполняют механизм сортировки «2d-массив» в этом массиве, который определен вне функции.Как использовать 2d-массив в другой функции? не знаю, как это сделать.
Мы нашли функцию онлайн, которая сортирует предопределенный массив внутри себя и пытается использовать эту функцию.
Наша проблема заключается в попытке использовать определяемый пользователем массив в функции.
пожалуйста, проверьте код ниже (обратите внимание, что мы не знаем, как использовать структуры)
Возникает вопрос: как мы используем наши orderListArray [] [] в функции сортировки массива?
#include <stdio.h>
#include <stdlib.h>
// define for sort array function later on
#define ARRAYSIZE(array) (sizeof(array)/sizeof(*(array)))
// function prototype
int sortArray();
int printOrderlist();
// data variables to be used throughout the code.
int itemNumber;
int itemAmount;
int maxItem = 0;
int lineCount = 0;
int priceToPrint = 0;
float totalPrice = 0;
// array we wish to implement into "sortArray" function
int orderListArray[][2];
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
int orderList()
{ // begin orderList
// makes sure user enters a maximum of 5 orders
int k = 0; // first place in array
int g = 0; // second place in array
do
{ // begin do1
printf("%d %d\n", k,g);
// asks for item number
puts("Enter item number (1-100):");
scanf("%d", &itemNumber);
// add scan to first spot (k) which is 0,0 (row 0, spot 0)
orderListArray[k][g] = itemNumber;
// add g++ to go to 0,1 (row 0, spot 1)
g++;
// asks for amount
printf("%d %d\n", k, g);
printf("You can order %d more items\n", 5-itemAmount);
printf("Enter amount:\n");
scanf("%d", &itemAmount);
maxItem = maxItem + itemAmount;
// add itemAmount to g which is 0,1
orderListArray[k][g] = itemAmount;
k++; // go to row 1 instead of row 0
g--; // go back to spot 0 in row.
// lineCount is used when we print rows of array since that is not predefined
lineCount++;
} // end do1
// runs as long as the total amount of items inputed matches.
while (maxItem <= 4);
return 0;
} // end orderList
//////////////////////////////////////////////////////////////////////////////// //////////
//////////////////////////////////////////////////////////////////////////////// //////////
int main(void)
{
orderList();
sortArray();
return 0;
}
//////////////////////////////////////////////////////////////////////////////// //////////
//////////////////////////////////////////////////////////////////////////////// //////////
// is used in sortArray() to sort 2d array.
int compare(const void *a, const void *b) {
int x1 = *(const int*)a;
int x2 = *(const int*)b;
if (x1 > x2) return 1;
if (x1 < x2) return -1;
// x1 and x2 are equal; compare y's
int y1 = *(((const int*)a)+1);
int y2 = *(((const int*)b)+1);
if (y1 > y2) return 1;
if (y1 < y2) return -1;
return 0;
}
//////////////////////////////////////////////////////////////////////////////// //////////
//////////////////////////////////////////////////////////////////////////////// //////////
// sortArray function (here we want to implement the orderListArray[k][g]
// and run on that instead of predefined matrix which is included in the code
int sortArray(int b[], size_t size)
{ // begin sortArray
int matrix[][2] = {{8,6}, {4,2}, {1,0}, {4,8}, {2,4},
{4,3}, {1,2}, {2,2}, {8,3}, {5,5}};
printf("Original: ");
for (size_t i = 0; i < ARRAYSIZE(matrix); i++)
printf("(%d,%d) ", matrix[i][0], matrix[i][1]);
putchar('\n');
qsort(matrix, ARRAYSIZE(matrix), sizeof(*matrix), compare);
printf("Sorted : ");
for (size_t i = 0; i < ARRAYSIZE(matrix); i++)
printf("(%d,%d) ", matrix[i][0], matrix[i][1]);
putchar('\n');
return 0;
} // end sortArray
// begin DescripANumbAPrice - неправильный комментарий. просто игнорируйте его. – theLittleBox
Знаете, вы можете отредактировать свой вопрос ...И у вас есть реальный вопрос здесь? –
вопрос был изменен. :) – theLittleBox