The task is to obtain data in the format - The name of the journal Index Buyer. Sort in this format - Index Naming Number of buyers. Find 2 magazines with the largest number of buyers.
struct edition{ //храним введенные данные char name[50]; //название ж int index; //индекс char subscr[50]; //Покупатель }; struct result{ //отсортированые данные int index; //индекс char name[60]; //название ж int count; // К-во покупателей }; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); int num, //к-во введенных значений i,j, //счетчики flag, //прапор, если елемента еще нет в структуре результатов pos; //значение последнего индекса в массиве структур результата printf("Какое к-во записей ?\n"); scanf("%d",&num); struct edition data[num]; printf("\nВведите: Название журнала Индекс Покупатель\n"); for(i=0;i<num;i++){ printf("%d. ",i+1); scanf("%s %d %s",data[i].name,&data[i].index,data[i].subscr); // запись в структуру } struct result res[num]; // инициализация структуры результата strcpy(res[0].name , data[0].name); // заносим 0 элемент в новий массив res[0].index = data[0].index; res[0].count = 1; pos = 0; //для того чтобы не было "пустых" элементов в массиве структур результата for(i=1;i<num;i++){ flag = 0; // записать элемент или нет for(j=0 ;j<sizeof(res)/sizeof(struct result);j++){ if( data[i].index == res[j].index ){ // если элемент уже записан res[j].count++;// инкрементируем к-во break; // next элемент }else{ flag = 1; // записать элемент pos++;// ичейка массива } } if( flag == 1 ){ // запись элемента в массив результатов strcpy(res[pos].name , data[i].name); res[pos].index = data[i].index; res[pos].count = 1; } } printf("%5s %12s %20s\n","Индекс","Назавание ","К-во покупателей\n"); for(i=0;i<sizeof(res)/sizeof(struct result);i++){ //выводим результат printf("%5d %12s %5d\n", res[i].index, res[i].name, res[i].count); } return 0; } But somewhere in the algorithm there is an error, because the calculation is wrong and, when outputting the results, it also displays the wrong result and abracadabra.
scanf("%s %d %s"- sing experience shows that it is better not to risk. Check from scanf. If this is not a student task - use sort f-tion - nick_n_a