Question

In: Computer Science

All Code should be written in C: 1. Write a C program which prompts the user...

All Code should be written in C:

1. Write a C program which prompts the user to enter two integer values. Your program should then print out all numbers between 1 and 1000 that are divisible by both of those numbers.

2. Modify your program from question 1 such that the first 1000 numbers that are divisible by both numbers are printed out, instead of numbers up to 1000.

3. Using dynamic memory, allocate memory for an array of 100 floating point numbers. Fill this array with random numbers between 0 and 1, and print them to the screen. HINT: Use RAND_MAX, which represents the largest possible value returned by the rand() function.

4. Modify question 3 such that the program writes the values to a file called "floats.txt" in text format, one per line, instead of to the screen

Solutions

Expert Solution

Here is the completed code for each part. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//CODE FOR PART 1

#include<stdio.h>

int main(){

                //declaring two integers

                int a, b;

                //asking for two integers

                printf("Enter two integer values: ");

                //reading two integers

                scanf("%d %d",&a,&b);

                //looping from i=1 to i=1000

                for(int i=1;i<=1000;i++){

                                //if i is divisible by a and b, printing it

                                if(i%a==0 && i%b==0){

                                               printf("%d\t",i);

                                }

                }

                printf("\n");

                return 0;

}

//CODE FOR PART 2

#include<stdio.h>

int main(){

                //declaring two integers

                int a, b;

                //asking for two integers

                printf("Enter two integer values: ");

                //reading two integers

                scanf("%d %d",&a,&b);

                int i=1; //starting value

                int counter=0; //count of numbers divisible by a and b

                //loops until counter hits 1000

                while(counter<1000){

                                //if i is divisible by a and b, printing it

                                if(i%a==0 && i%b==0){

                                               printf("%d\t",i);

                                               //incrementing counter

                                               counter++;

                                }

                                //next value

                                i++;

                }

                printf("\n");

                return 0;

}

//CODE FOR PART 3

#include<stdio.h>

#include<stdlib.h>

int main(){

                //creating a dynamic float array of 100 elements

                float *arr = (float*) malloc(100 * sizeof(float));

                //looping from i=0 to i=99

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

                                //generating a random number between 0 and 1, assigning to arr[i]

                                arr[i]=(rand()/(float)RAND_MAX);

                                //printing the number

                                printf("%f\n",arr[i]);

                }

                //de allocating memory

                free(arr);

                return 0;

}

//CODE FOR PART 4

#include<stdio.h>

#include<stdlib.h>

int main(){

                //creating a dynamic float array of 100 elements

                float *arr = (float*) malloc(100 * sizeof(float));

                //opening a file 'floats.txt' in write mode

                FILE* op=fopen("floats.txt","w");

                //looping from i=0 to i=99

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

                                //generating a random number between 0 and 1, assigning to arr[i]

                                arr[i]=(rand()/(float)RAND_MAX);

                                //printing the number to the file

                                fprintf(op,"%f\n",arr[i]);

                }

                //de allocating memory

                free(arr);

                //closing file, saving changes

                fclose(op);

                return 0;

}


Related Solutions

Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
This needs to be a python3 code Write a program that prompts the user like this:...
This needs to be a python3 code Write a program that prompts the user like this: “Currency to convert to U.S. dollars: e = Euros, c= Chinese Yuan, r = Indian Rupees, b = Bitcoin: ”. Then depending on which letter the user enters, the program displays “Amount of Euros/Yuan/Rupees/Bitcoin to convert: ”. (Note: the second prompt should only name the one currency the user asked to convert, not all four currencies.) After the user enters the amount, the program...
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT