WHAT THE PROGRAM DO:
I am making a program that displays how many times a number has been entered by the user. It will stop asking value if a number less than one has been entered.
SCREENSHOT AND EXAMPLE OF THE PROGRAM
I was able to create the program by initializing the value of the array "count" to 100. SCREENSHOT OF WHAT I DID.
The issue with this program, is that it will only accept values until 100. It will not accept values more than 100. This is a screenshot if more than a hundred value has been entered: SCREENSHOT OF MORE THAN 100
THE PROBLEM
This is where I want realloc() to come in. I want the to change the malloc() size depending on the highest entered value so it will be more flexible using realloc(). SCREENSHOT OF WHAT I CHANGED IN THE PROGRAM TO USE REALLOC()
However, doing so destroys the program. SCREENSHOT OF THE NEW OUTPUT OF THE PROGRAM
Please help me.
MY PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <stdbool.h>
main()
{
//DECLARATION OF VARIABLES
int i, j, k, highestValue=1, size=1;
int* input = (int*)calloc(size, sizeof(int));
int* count = (int*)calloc(highestValue, sizeof(int));
bool iCondition = true;
//USER INPUT
for (i=0; iCondition==true; i++)
{
//GETS USER INPUT
printf("Enter a number: ");
scanf("%d", &input[i]);
//CHECKS IF THE NUMBER ENTERED IS A HIGH NUMBER
if (highestValue<input[i]){
highestValue = input[i];
count = realloc(count, highestValue * sizeof(int));
}
//CHECKS HOW MANY TIMES THE NUMBER HAS BEEN ENTERED
bool jCondition = true;
for(j=0; jCondition==true; j++)
{
if (input[i] == j){
count[j-1]++;
jCondition=false;
}
}
//ENDS THE LOOP IF THE ENTERED NUMBER IS LESS THAN 1
if(input[i] < 1)
iCondition = false;
//IF NOT, THIS WILL REALLOCATE/CHANGE ARRAY SIZE BY ADDING +1!!
else{
size++;
input = realloc(input, size * sizeof(int));
}
}
//PRINTS OUTPUT | USES THE HIGHESTVALUE AS THE CONDITIONAL EXPRESSION FOR FLEXIBILITY
for (i=0; i<=highestValue; i++)
{
//PRINTS ALL NUMBER THAT IS NOT EQUAL TO ZERO(0)
if (count[i] != 0)
printf("\n %d was entered %d time/s ", i+1, count[i]);
}
getch();
}