Question

In: Computer Science

#include <stdio.h> #include <stdlib.h> #include <time.h> int main() {    srand(time(NULL)); // randomize int *a; // pointer...

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {   

srand(time(NULL)); // randomize

int *a; // pointer to array

int i, n;

printf("Size of array:");

scanf("%d", &n);

/* memory allocation */

a = (int*)malloc(n*sizeof(int));

/* array generating */

for (i = 0; i < n; i++) a[i] = rand()%101;

/* output */

for (i = 0; i<n; i++) printf("a[%d] = %d ", i, a[i]);

putchar('\n');

free(a);

return 0;  

}

Using the above code please do it to me following

  1. #1: Study Programming at the Kernel Level
    Write a program that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum. It then writes the numbers to a new file using open, close and write.
    Other requirements:
  1. Input—The program has no input data (it uses standard function to generate sequence of random numbers).
  2. Output—The program prints the sum of the array. The program creates a file called numbers.XXXX where XXXX is the sum of the numbers in the file.
  3. Program structure—The program has needs only one function: int main(void)

Hints:

Remember to initialize any sum variable to 0

rand() will return a random number too big for 0-99. Use modulo to reduce the range.

rand() needs to be initialized with a seed. You can use: srand(time(0));

  1. #2: Write a program that looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum of the integers.

Hints:

Make a function to deal with open/read/sum/print/close.

opendir() requires a starting directory. You want to start where in the current directory.

You need to loop over the directory entries. This is similar to walking a linked list.

You want to check every file entry to see if it starts with “numbers.”

Solutions

Expert Solution

CODE TO COPY ALONG WITH SCREENSHOTS ARE PROVIDED BELOW:

(FOR INDENTS REFER TO SCREENSHOTS)

<<<<<<<<<<<-------------SOLUTION FOR RZRK #1--------------------------->>>>>>>>>>>>>>>>>.

SCREENSHOTS OF CODE:

OUTPUT SCREENSHOT:

After running the program txt file gets created.

(Each time code will run it will output the sum of random numbers like output screenshot above, And it will create txt file containing 100 random numbers in each file)

CODE FOR COPYING :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>


int main(void)
{


char s1[] = "numbers.";
char s2[10];
char s3[] = ".txt";

int sum = 0;
int a[100];

srand ( time(NULL) );

for(int i =0;i<100;i++){

a[i] = rand()%101;
}


for(int i=0;i<100;i++){

sum = sum + a[i];
}
sum = sum + a[99]; /*adding last number to sum as it didnt got added in loop*/

printf("SUM : %d\n",sum);

sprintf(s2,"%d",sum); /*converting sum to a string */

strcat(s1,s2); /*adding all strings together and storing into s1 */
strcat(s1,s3);


FILE *fp;
fp = fopen(s1,"w"); /*creating file */

for(int i =0;i<100;i++){

fprintf(fp,"%d ",a[i]); /*storing numbers into file */
}
fclose(fp);

return 0;
}

<<<<<<<<<<<<<<<<<<<<----------------SOLUTION FOR RZRK #2 ----------------->>>>>>>>>>>>>>>

SCREENSHOTS:

output screeenshots:

Code to copy :

#include <stdbool.h>
#include <stdio.h>
#include <dirent.h>

bool StartsWith(const char *a, const char *b)
{
if(strncmp(a, b, strlen(b)) == 0) return 1;
return 0;
}
void action(const char *s){

FILE *file = fopen(s,"r");
int sum = 0;
int x;
while(!feof(file))

{

fscanf(file,"%d",&x);

sum+=x;

}
fclose(file);
printf("FILE_NAME: %s\n",s);
printf("SUM: %d\n\n",sum);

}


int main(void)
{
struct dirent *de; // Pointer for directory entry

// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");


// for readdir()
while ((de = readdir(dr)) != NULL){
if(StartsWith(de->d_name,"numbers.")){
action(de->d_name);

}
}
closedir(dr);
return 0;
}


Related Solutions

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size);...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size); int main(){ int arraySize, limit, count, srand(time(0)); print f("Enter the size of array\n"); scanf("%d", arraySize); int array[arraySize]; printf("Enter the upper limit\n"); scanf("%d", &limit); count = 0; while(count <= arraySize){ array[count] = (rand() % (limit + 1)); count++; } printArray(array, &arraySize); sort(array, arraySize); printArray(array, arraySize); Return 0; } void printArray(int a[], int size){ int i = 0; printf("Array: ["); while(i < size){ if(i != size...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
#include<stdio.h> #include<stdlib.h> int main() {     //Q1) Note: You can use only pointer based operations while...
#include<stdio.h> #include<stdlib.h> int main() {     //Q1) Note: You can use only pointer based operations while implementing each of the functionalities below    // zero points will be given if pointer operations are not used in implementing    // each of the operations below.    // Also, use C code only in visual studio or GCC in general.asu.edu server    /* i.) Create a 2 - D array of integers using pointers (use dynamic memory allocation).            Assume that...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT