Good day! Trying to accomplish this task:
Ввести n послідовностей стрингових констант різної довжини. Вирівняти їх у відповідності з довжиною найбільш короткої. Представити їх у вигляді символьної матриці. Виконати установку заданих елементів матриці у задане значення. As far as I understand, you just need to sort the array (two-dimensional) rows, so that the first row is the shortest length. And then just bring out by the letter.
Here is the code, but I can't get an array. Although it is possible there are many more errors than I think, VS2013 says only about the fact that the conclusion at the end is not correct (for the campaign, I’ll fix it somewhere else). How to do it, please help the community!
Code:
#include <iostream> #include <string.h> #include <string> #include <cstdio> #include <cstdlib> #include <locale.h> #include <conio.h> using namespace std; int compare(const void *, const void *); // это прототип ф-ии, он нужен что бы не писать всю ф-ию выше main'а, при этом видеть что за ф-ия у тебя и какие в ней аргументы {ее описание ниже, после main} int main() { setlocale(LC_ALL, "RUS"); int n; // кол-во строк cout << "input amount of strings "; cin >> n; cout << "Введите " << n << " размеров для каждой строки: " << endl; int *symb = new int[n]; // массив для хранения длины каждой новой строки для массива строк for (int i = 0; i < n; ++i) cin >> symb[i]; char **str_mas = new char *[n]; for (int i = 0; i < n; ++i) str_mas[i] = new char [symb[i]+1]; /*for (int i = 0; i < n; ++i) { // вводим строки для твоего массива двумерного char cout << "Введите строку номер " << i + 1 << ": "; for (int j = 0; j < symb[i]; j++) cin >> str_mas[i][j]; }*/ cin.clear(); cin.ignore(); for (int i = 0; i < n; ++i) { cout << "Введите строку номер " << i+1 << ": "; cin.getline(str_mas[i], symb[i]+1); } char **new_str_mas = new char*[n]; // в этот массив копируем строки из первого в порядке возрастания длины строк for (int i =0; i < n; ++i) new_str_mas[i] = new char[symb[i]]; memset(new_str_mas, 0, sizeof(new_str_mas)); // ф-ия для очистки массива char от мусора qsort(symb, n, sizeof(int), compare); // ф-ия сортировки, нужна что бы был массив с размерами каждой строки char отсортирован, дальше нужен для пермещения строки из старого массива строк в новый //strlen() - происходит проверку строки с соответствующим размером из массива, где // хранятся размеры строк, и если размер совпал - копирование в новый массив строк for (int i = 0; i < n; i++) for(int j = 0; j < n; ++j) { if (strlen(str_mas[j]) == symb[i]) { strcpy_s(new_str_mas[j], strlen(str_mas[i]), str_mas[i]); } } // НУЖЕН ВЫВОД for (int i = 0; i < n; i++) for (int j = 0; j < symb[i]; ++j) { cout << new_str_mas[i][j]; if (j == symb[i] - 1) cout << endl; } _getch(); } // эта ф-ия вспомогательная для стандартной ф-ии сортировки qsort(см.выше), без нее qsort doesnt work! int compare(const void * a, const void * b) { return (*(int*)a - *(int*)b); }