Question
Need this in C
Code is given below

e Dots l lah dit Problem 1. (30 points) Fre bendord.cto obtain the free in decimal representation For ATY. this problem we co
Co... st... Ma... st unt... benford.co 1 #include <stdio.h> 2 #include <stdlib.h> 4 #define MAX_N 10000e 5 #define N_DIGITS 1
nd Help unt... benford.co co... @st... Ma... sto.. -4 // initialize an array 15 // the function does not return a value. 46 v
in(int argc, char ** argv) nt n = MAX N; f (argc >= 2) { n = atoi(argv[i]); if (n<ill n > MAX_N) { printf(the integer must b
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include<stdlib.h>
#define MAX_N 100000
#define N_DIGITS 10//this value can be anything even we can define through command line
void update_frequency(unsigned long k,unsigned long freq[])//update the frequencies with respect to k
{
int r;
while(k!=0)//take each value
{
r=k%10;
freq[r]++;//increment that value frequency
k=k/10;//next number
}
}
void frequencies(unsigned long a[],int n,unsigned long freq[])
{
for(int i=0;i<n;i++)
{
int t=a[i];//take first value
while(t!=0)//loop through each digit
{
int r=t%10;
freq[r]++;//update that digit frequency
t=t/10;
}
}
}
void print_freq(unsigned long freq[])
{
for(int i=0;i<N_DIGITS;i++)
{
printf("%2d:%-5u",i,freq[i]);//print every frequency value
}
printf("\n");
}
void init_array_uniform(unsigned long a[],int n)
{
for(int i=0;i<n;i++)//this is uniform array it will have numbers in sequence order 1,2,3....
a[i]=i+1;
}
void init_array(unsigned long a[],int n)//it follows some pattern
{
a[0]=1;
for(int i=1;i<n;i++)
a[i]=a[i-1]+i+1;
}
int main(int argc,char **argv)
{
int n=MAX_N;
if(argc>=2)
{
n=atoi(argv[1]);//converting the cmd argument to int
if(n<1||n>MAX_N)
{
printf("The integer must be >=1 and <=%d\n",MAX_N);
return -1;
}
}
unsigned long a[n];
unsigned long freq[N_DIGITS];
for(int i=0;i<N_DIGITS;i++)
freq[i]=(unsigned long)-1>>1;//putting -1 as initial values
init_array_uniform(a,n);//uniform array initialization
frequencies(a,n,freq);//calculate frequencies
print_freq(freq);//print frequencies
printf("updated\n");
update_frequency(6734,freq);//update frequencies
print_freq(freq);//print frequencies
printf("\n");
init_array(a,n);//normal array initialization
frequencies(a,n,freq);//calculate frequencies
print_freq(freq);//print frequencies
printf("updated\n");
update_frequency(43562,freq);//update frequencies
print_freq(freq);//print frequencies

return 0;
}

//I have done with value 10 you can change any value you want in define place or pass from command line arguments

0:38893 1:50000 2:49999 3:49999 4:49999 5:49999 6:49999 7:49999 8:49999 9:49999 updated 0:38893 1:50000 2:49999 3:50000 4:500

Add a comment
Know the answer?
Add Answer to:
Need this in C Code is given below e Dots l lah dit Problem 1. (30...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of...

    //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of the numbers in an array of long integers #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <assert.h> //#define NUM_THREADS 2 #define DIGITS 10 #define MAX 100000000 unsigned long a[MAX]; struct thread_data { int thread_num; int i, j;                           //the staring and ending index unsigned long freq[DIGITS];           // results }; //initialize the array void init_array(unsigned...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • this is c code. please answer all questions on a piece of paper and show work....

    this is c code. please answer all questions on a piece of paper and show work. i need to prepare as i have a midterm i will have to be completing on paper 1) Bit Operators: This C program compiles and runs. What is its output? 1) #include <stdio.h> 2) void main (void) 3) unsigned char x =60; 4) 5) 6) 7) 8 ) 9) 10) 11) 12) 13) unsigned char a = x < 1; unsigned char b unsigned...

  • so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n)...

    so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n) using semaphores implement solution to the critical section problem #include #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • In this exercise we “reverse-engineer” some code to try to determine how the heap in a C program ...

    In this exercise we “reverse-engineer” some code to try to determine how the heap in a C program is managed. Consider the following C program (compiled as an executable called memory): #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { int chunk_sizes[4]; if ((argc != 2) || (sscanf(argv[1], "%d,%d,%d,%d", &chunk_sizes[0], &chunk_sizes[1], &chunk_sizes[2], &chunk_sizes[3]) != 4) || (chunk_sizes[0] < 0) || (chunk_sizes[1] < 0) || (chunk_sizes[2] < 0) || (chunk_sizes[3] < 0) ) { fprintf(stderr,"Usage: %s a,b,c,d\n", argv[0]); fprintf(stderr," where...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long...

    What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long digitCount=0; long lineCount=0; long wordCount=0; long digitFreq[10]; int c=0; int outOfWord=0; int state=0; int inWord=1; void printStats(); void totalWords(); void totalLines(); void digitFrequency(); int main(int argc,char **argv) { int i; for(i=0;i<10;i++) { digitFreq[i]=0; } state=outOfWord; c=getchar(); while((c !=EOF)) { charCount++; digitFrequency(c); totalLines(c); totalWords(c); c=getchar(); } printStats(); return 1; } void totalLines(int c) { if(c == '\n') { lineCount++; } } void digitFrequency(int c) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT