C/C++

Написать программу на с++

Написать программу:
Ввод с клавиатуры целых чисел в динамический массив. Признак конца - ввод числа 999. Найти среди введенных чисел наиболее часто встречающееся число. Условие: выделение памяти должно быть реализовано через malloc/realloc
Faust !
Faust !
96
#include<stdio.h> //вкл. ввод-вывод
#include<stdlib.h> //вкл. malloc,realloc
#include<unistd.h> //вкл. паузу

int main()
{
int *vector, often,
count=1, copy=1;
vector=(int*)malloc(sizeof(int));
if(!vector) exit(1);

while(1)
{
printf("\nEnter your number: ");
scanf("%d",&vector[count-1]);
if(vector[count-1] == 999) break;
vector=(int*)realloc(vector,++count*sizeof(int));
if(!vector) exit(1);
}
--count;
printf("\n\nResult dynamic array:\n");
for(int i = 0; i < count; i++)
printf(" %d", vector[i]);

for(int i = 1; i < count; i++)
if(vector[0] == vector[i])
copy++;
often=vector[0];

for(int i = 1; i < count; i++)
{
int jcopy=1;
for(int j = 1; j < count; j++)
{
if(vector[i]==vector[j]
&&i!=j) jcopy++;
}
if(jcopy > copy)
{copy=jcopy; often=vector[i];}
}
printf("\n\nOften number is %d, %d counts", often, copy);
free(vector);
fflush(stdout);//сброс буфера для паузы
sleep(5); //пауза
return 0;
}
МС
Марат Салимов
37 945
Лучший ответ
Faust ! спасибо, то что надо))
#include <iostream>
#include <cstdlib>

using namespace std;

struct N
{
int num;
int count;
};

int main()
{
int size;
cout << "Введите максимальный размер массива: ";
do
{
cin >> size;
} while(size < 1);
int * A;
A = (int*) malloc(sizeof(int) * size);
int x;
for(x = 0; x < size; x++)
{
cin >> A[x];
if(A[x] == 999) break;
}

N n[x];
for(int y = 0; y < x; y++)
{
n[y].num = A[y];
n[y].count = 0;
for(int z = 0; z < y; z++)
if(A[z] == A[y]) n[y].count++;
}

int max = 0;
for(int y = 0; y < x; y++)
{
if(n[y].count > n[max].count) max = y;
}

cout << "Наиболее часто встречается число ";
cout << n[max].num << '.' << endl;
if(A != (int*) nullptr)
{
free(A);
A = (int*) nullptr;
}
return 0;
}