#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
Need this in C Code is given below e Dots l lah dit Problem 1. (30...
//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 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 < 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 <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. 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) 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 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 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 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 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) {...