So this is the code I am trying to run with Code:block; I have no idea what is w
ID: 3820325 • Letter: S
Question
So this is the code I am trying to run with Code:block;
I have no idea what is wrong with it. Can you please fix it so it can be run. Please go over step by step. I am trying to learn this program. Thank you.
#include <stdio.h>
#include <stdlib.h> // For random(), RAND_MAX
// Assumes 0 <= max <= RAND_MAX
// Returns in the closed interval [0, max]
int random_at_most(int max) {
unsigned long
// max <= RAND_MAX < ULONG_MAX, so this is okay.
num_bins = (unsigned int) max + 1,
num_rand = (unsigned int) RAND_MAX + 1,
bin_size = num_rand / num_bins,
defect = num_rand % num_bins;
int x;
do {
x = random();
}
// This is carefully written not to overflow
while (num_rand - defect <= (unsigned int)x);
// Truncated division is intentional
return x/bin_size;
}
int main(void)
{
int random_at_most(int max);//decleation of random no generation function
int n,i;//n= no of elements.
int a[1000],b[100];//array declared to store generated random no.
printf("enter number of elements ");
scanf("%d",&n);
//generating random no n times and storing in array
for( i=0;i<n;i++)
{
a[i]=random_at_most(9);
}
//print the generated random elements
printf(" the generated random elements ");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
//copying array to data.txt file
/*
FILE *myFile;
myFile = fopen("data.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(myFile, "%;d", &a[i]);
}
fclose(myFile)
//reading data from data.txt
FILE *myFile;
myFile = fopen("data.txt", "r");
//read file into array
for (i = 0; i < n; i++)
{
fscanf(myFile, "%d", &b[i]);
}
for (i = 0; i < n; i++)
{
printf("Number is: %d ", b[i]);
}
fclose(myFile);
*/
int inputArray[100], countArray[100];
int elementCount, j, count;
elementCount=n;
/* Read array elements */
for(i = 0; i < elementCount; i++){
inputArray[i]=a[i];
countArray[i] = -1;
}
/*
* for any element inputArray[i], If countArray[i] = -1,
* that means frequency is not counted for this number yet
* and countArray[i] = 0 means frequency is already
* counted for this number.
*/
for(i = 0; i < elementCount; i++) {
count = 1;
for(j = i+1; j < elementCount; j++) {
if(inputArray[i]==inputArray[j]) {
countArray[j] = 0;
count++;
}
}
if(countArray[i]!=0) {
countArray[i] = count;
}
}
/* Print count of each element */
for(i = 0; i<elementCount; i++) {
if(countArray[i] != 0) {
printf(" Element %d : Count %d", inputArray[i], countArray[i]);
}
}
return 0;
}
Explanation / Answer
Answer: Your program is entirely correct except for one line that is you sould use rand() instead of random() in C to generate random numbers. Working code is pasted below:
#include <stdio.h>
#include <stdlib.h> // For random(), RAND_MAX
// Assumes 0 <= max <= RAND_MAX
// Returns in the closed interval [0, max]
int random_at_most(int max) {
unsigned long
// max <= RAND_MAX < ULONG_MAX, so this is okay.
num_bins = (unsigned int) max + 1,
num_rand = (unsigned int) RAND_MAX + 1,
bin_size = num_rand / num_bins,
defect = num_rand % num_bins;
int x;
do {
x = rand();
}
// This is carefully written not to overflow
while (num_rand - defect <= (unsigned int)x);
// Truncated division is intentional
return x/bin_size;
}
int main(void)
{
int random_at_most(int max);//decleation of random no generation function
int n,i;//n= no of elements.
int a[1000],b[100];//array declared to store generated random no.
printf("enter number of elements ");
scanf("%d",&n);
//generating random no n times and storing in array
for( i=0;i<n;i++)
{
a[i]=random_at_most(9);
}
//print the generated random elements
printf(" the generated random elements ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
//copying array to data.txt file
/*
FILE *myFile;
myFile = fopen("data.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(myFile, "%;d", &a[i]);
}
fclose(myFile)
//reading data from data.txt
FILE *myFile;
myFile = fopen("data.txt", "r");
//read file into array
for (i = 0; i < n; i++)
{
fscanf(myFile, "%d", &b[i]);
}
for (i = 0; i < n; i++)
{
printf("Number is: %d ", b[i]);
}
fclose(myFile);
*/
int inputArray[100], countArray[100];
int elementCount, j, count;
elementCount=n;
/* Read array elements */
for(i = 0; i < elementCount; i++){
inputArray[i]=a[i];
countArray[i] = -1;
}
/*
* for any element inputArray[i], If countArray[i] = -1,
* that means frequency is not counted for this number yet
* and countArray[i] = 0 means frequency is already
* counted for this number.
*/
for(i = 0; i < elementCount; i++) {
count = 1;
for(j = i+1; j < elementCount; j++) {
if(inputArray[i]==inputArray[j]) {
countArray[j] = 0;
count++;
}
}
if(countArray[i]!=0) {
countArray[i] = count;
}
}
/* Print count of each element */
for(i = 0; i<elementCount; i++) {
if(countArray[i] != 0) {
printf(" Element %d : Count %d", inputArray[i], countArray[i]);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.