Starting from:

$25

CSE102 - Computer Programming - Homework 3  - Solved

 Write a complete program that performs following tasks respectively. These tasks should be distinct functions and called from main() function with selection. Once they been called, each will call another function to make the calculation for the related task. These calls will be made by selecting the inputs received from the user. For each process, the input / output part and the calculation part will be created separately. So you have to do the calculations in a separate function. This function should take the values required for calculation from the previous function as input and return output as a result. You should create a menu as shown below.

Expected Menu 

  

Part 1. [25 pts] In this function, the program respectively request a two Integer (N1, N2), an operation flag, and finally a flag to decide if it is going to work on odd / even numbers. The program will support only 2 operations, addition or multiplication. According to the selection of the flags, the program will calculate the sum/multiplication of the odd/even numbers between the range of [N1,N2]. The addition and the multiplication operations should be done by using two different functions. The operation selection should be determined by using operation flag with switch-case, after that, the integer and the other flag should be used to call the related function. If user enters invalid value for the flags, the program should print an error message. After each calculation process is completed, the result should be written to the file named “results.txt”.

Function prototypes are : 

int sum (int n1, int n2, int flag) int multi (int n1, int n2, int flag) 

Examples: 

  

  

Part 2. [25 pts] Write a function which takes an integer N from the user and checks the every integer from 2 to that number if they are prime or not. The primality testing is made by a function with the following information:

A is prime if A is not dividible by any integer X where X is;  

 

1 < 𝑋 ≤ √𝐴 

This operation should be done in a function, the function should return a flag if the integer is prime, or should return the least divisor of that integer if it is not a prime. In the main function, you should use a loop to check every A between 1<𝐴<𝑁 obtain a result by using the function and finally print it.

Only ‘for’ loops should be used. You are allowed to use sqrt() function from math library to calculate the square root.

Function prototype is :  int isprime (int a) Expected Output 

  

 

Part 3. [50 pts]: In file operations, you must create a function named write_file() for writing to the file. This function should take the value to be written to the file as a parameter. The 3rd option on the menu should call the print_file() function and print the numbers in the "results.txt" to the terminal. The 4th option in the menu should call the function named sort_file(). In this function, a simple sorting operation should be performed in ascending order in the "results.txt" file. In this sorting process, you need to sort the mixed numbers in the results.txt file from smallest to largest. You should select 3 numbers in each step. You should determine the smallest three numbers in sequence and keep them on a temporary file. In other words, you should assign the smallest numbers to the min1, min2, min3 variables and keep them in order from smallest to largest in a temporary file. A sample image of this is below. In each sorting round, the smallest 3 numbers should be determined and transferred to the temporary file. You are not allowed to use arrays. You can create the necessary functions by adding your comment lines. When you complete all the operations, you should print all the numbers in order from smallest to largest on "results.txt". Remember, you need to sort the numbers 3 by 3, not one by one.

 

void write_file (int number)  void sort_file ()  void print_file () 

Expected Outputs :  

More products