cmpt
.pdf
keyboard_arrow_up
School
Simon Fraser University *
*We aren’t endorsed by this school
Course
125
Subject
Computer Science
Date
Apr 3, 2024
Type
Pages
4
Uploaded by SargentEchidnaPerson2082
Assignment 1 CMPT 125 Intro. To Computing Science & Programming II Fall 2023 Page 1 of 4 © Victor Cheung, 2023 Assignment 1 (5% of Course Total) Due date: 11:59pm, Sep 29, 2023 Part of the assignment will be graded automatically. Make sure that your code compiles without warnings/errors and produces the required output. Also use the file names and structures indicated as requested. Deviation from that might result in 0 mark. Your code MUST compile and run in the CSIL machines with the Makefile provided. It is possible that even with warnings your code would compile. But this still indicates there is something wrong with you code and you have to fix them, or marks will be deducted. Your code MUST be readable and have reasonable documentation (comments) explaining what it does. Use your own judgement, for example, no need to explain i += 2 is increasing i by 2, but explain how variables are used to achieve something, what a certain loop is doing, and the purpose of each #include. Description There is a total of 3 questions in this assignment. For each question, write your answer in a single file that contains your student information as comments at the top. Unless otherwise specified, do not include any pre-existing libraries in your answers
. You can however write your own helper functions. Also, do not print anything unless the question asks you to. None of these files should contain the main function
. Question 1 [5 marks] Write a function that “shuffles” the digits in an unsigned int number and returns the result as an unsigned int with this rule: swap the first digit with the last digit, swap the second digit with the second last digit, …etc. Use this function header:
unsigned int shuffleDigits(unsigned int number) For example: shuffleDigits(1) should return 1 shuffleDigits(22) should return 22 shuffleDigits(123) should return 321 shuffleDigits(1223) should return 3221 shuffleDigits(5670) should return 765 (leading zero will not be part of the resulting number) shuffleDigits(30400) should return 403 (leading zeros will not be part of the resulting number) You can assume the number does not have leading zeros, and we will not test shuffleDigits(0). Only include the function definition (and your helper functions, if any) in the source file and name it as a1_question1.c
. Do not use recursion in your answer. Question 2 [5 marks] Write a function that takes in 4 parameters: an int array, its size, the left index, and the right index; and sorts the elements between the left and right index (inclusive) in the int array in ascending order. If the left index is larger than the right index, swap them first. Then, if the left index is invalid (e.g., negative, larger that the size), use the leftmost valid index of the array; if the right index is invalid, use the rightmost valid index of the array. Use this function header:
Assignment 1 CMPT 125 Intro. To Computing Science & Programming II Fall 2023 Page 2 of 4 © Victor Cheung, 2023 void rangedSort(int array[], unsigned int size, int leftIndex, int rightIndex) For example* (suppose there is a myIntArray created as [-4, 3, -12, 0, 5, 72, 88, 128, 1, 64]): rangedSort(myIntArray, 10, -2, 3) will change myIntArray into [-12, -4, 0, 3, 5, 72, 88, 128, 1, 64] rangedSort(myIntArray, 10, 10, 7) will change myIntArray into [-4, 3, -12, 0, 5, 72, 88, 1, 64, 128] rangedSort(myIntArray, 10, 5, 5) will change myIntArray into [-4, 3, -12, 0, 5, 72, 88, 128, 1, 64] rangedSort(myIntArray, 10, 0, 9) will change myIntArray into [-12, -4, 0, 1, 3, 5, 64, 72, 88, 128] *each example works on the original myIntArray. You can assume the array has one or more elements and the size is always correct. For the sorting, you can choose between Insertion Sort and Selection Sort (state your choice in the comments). Only include the function definition (and your helper functions, if any) in the source file and name it as a1_question2.c
. Do not use recursion in your answer. Question 3 [6 marks] Suppose we use a 1D array of 3 ints to represent a point (e.g., [1, 2, 1] represents (1, 2, 1)), then we can use a N-by-3 2D array to represent N points, where each row represents 1 point. In mathematics, the Euclidean distance between 2 points (x1, y1, z1) and (x2, y2, z2) is calculated as: √(?
1
− ?
2
)
2
+ (?
1
− ?
2
)
2
+ (?
1
− ?
2
)
2
Write a function that takes in the number of rows (number of columns is fixed at 3) of a 2D array of ints, the 2D array itself, a 1D array of 3 ints; and prints the Euclidean distances from each row of the 2D array (representing 1 point) to the point represented by the 1D array. This function should also return the total distance as a float
. Use this function header: float printPointsDistances(unsigned int row, int points[][3], int point[]) For example, suppose we have 4 points: (0, 0, 0), (0, 2, 0), (2, 0, 0), and (2, 2, 2), and 1 point at (2, 2, 0). Then the function will print the following to the console (distances are in 4 decimal places): Euclidean distance from (0, 0, 0) to (2, 2, 0) is 2.8284 Euclidean distance from (0, 2, 0) to (2, 2, 0) is 2.0000 Euclidean distance from (2, 0, 0) to (2, 2, 0) is 2.0000 Euclidean distance from (2, 2, 2) to (2, 2, 0) is 2.0000 It will also return the total distance (8.828427) to the caller of this function. You can assume that the 2D array will have the correct number of columns and the number of rows is correct. You can also assume that the 1D array will have the correct number of int elements (3). Do not use the pow function from the math.h library in your answer
(if you do you get 0 for this question). Only include the function definition (and your helper functions, if any) in the source file and name it as a1_question3.c
.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
Question No:
2012123nt505
2This is a subjective question, hence you have to write your answer in the Text-Field given below.
76610
A team of engineers is designing a bridge to span the Podunk River. As part of the design process,
the local flooding data must be analyzed. The following information on each storm that has been
recorded in the last 40 years is stored in a file: the location of the source of the data, the amount of
rainfall (in inches), and the duration of the storm (in hours), in that order. For example, the file
might look like this:
321 2.4 1.5
111 33 12.1
etc.
a. Create a data file.
b. Write the first part of the program: design a data structure to store the storm data from the file,
and also the intensity of each storm. The intensity is the rainfall amount divided by the duration.
c. Write a function to read the data from the file (use load), copy from the matrix into a vector of
structs, and then calculate the intensities.
(2+3+3)
arrow_forward
Topics: Functions, Files Read and write, DictionarySuppose you are given an input file of a document that only contains English words, spaces, commas(always followed with one space) and periods (always followed with one space). Your task is to readthe file, count the word frequency by ignoring the letter case, output the frequently used words (i.e.,the words occurred more than once) and the most frequently used word among all of them.Lab Scenario: Count the word in a document1. The program reads from an already provided input file: “document.txt”, which containsseveral paragraphs separated by an empty line.2. You will perform the file open operation. And then perform the read operation with your choice ofread functions and read the content of the file.3. Once you are done reading, start processing the contents of the file using a dictionary where thekey would be the word in lowercase and the corresponding values would be word frequency whichwill be the number of occurrences in the…
arrow_forward
Introduction:It is often necessary to reformat data in files. Sometimes it is done for readability, sometimes it is done to fit as input to different programs.
Purpose:The purpose of the task is to provide proficiency in simple text management and file management.The task is the basis for file management.
Reading instructions:You should be done with all the steps up to and including file management.
Implementation:
You will create a standalone program that performs the tasks below. Start by solving task one and complete that task before starting task 2.
Your program should read the files mentioned from the "current working directory".All files are in text format with "\ n" at the end of the line. If you wish, you can use pipes and redirects to solve the file reading and printing instead of std :: ifstream and std :: ofstream.Comment on it in the code along with examples of how the program is used.
The file names.txt contains names and social security numbers in the following form. The…
arrow_forward
The listing file is identified by
a) source file name
b) extension .LSF
c) source file name and an extension .LSF
d) source file name and an extension .LST
arrow_forward
Python
arrow_forward
C# please
Write a program that will create file with at least 10 lines. Each line of your file should have information delimited by some delimiter (I.e., commas, spaces, slashes, symbols, etc.). After your file has been created with the proper amount of data, you should read your file line by line and display without the delimiters in the same program after closing your writer. Additionally, the information stored into your file should be data that is gathered from a separate class.
Your program should save your file in a specific directory. To promote flexibility, your program should check to see if the chosen directory exists prior creating it, and if it does not exist, your program should create it for you. Additionally, you should allow the user to enter the names of the directory and file instead of hard-coding it into your program.
arrow_forward
I just need help forming the source code! any help would be great!
You are working on development of a new water treatment plant for Spokane, Washington. Monthly rainfall data has been collected as part of designing the new water treatment plant, and you are required to develop a computer program that will perform statistical analysis. The input data file is called rainfall. As you examine the input file, you notice that the first record line is the name of the city and type of data, the second record line contains the control numbers, and the remaining record lines are the average monthly rainfall amounts: 12 rows (January – December) and 6 years (2015 – 2020).
Write a C program that will read the data from the input file, such that the city name and type of data are stored in a one-dimensional character array, and the rainfall amounts are stored in a two-dimensional array. Manipulate the two-dimensional array to calculate the average rainfall for each year and the standard deviation…
arrow_forward
A file has r = 20, 000 STUDENT records of fixed length. Each record has the following fields: NAME (30 bytes), SSN (9 bytes), ADRESS(40 bytes), PHONE(9 bytes), BIRTHDATE (8 bytes), SEX(1 byte), CLASSCODE( 4 bytes, integer) MAJORDEPTCODE(4 bytes), MINORDEPTCODE(4 bytes), and DEGREEPROGRAM( 3 bytes). An additional byte is used as a deletion marker. Block size B = 512 bytes.
a) Calculate the blocking factor bfr (=floor(B/R), where R is the record size) and number of file blocks b, assuming unspanned organization (a record can’t be split across blocks).
b) Suppose only 80% of the STUDENT records have a value for PHONE, 85% for MAJORDEPTCODE, 15% for MINORDEPTCODE, and 90% for DEGREEPROGRAM. We use a variable-length record file. Each record has a 1-byte field type for each field in the record, plus the 1-byte deletion marker and a 1-byte end-of-record marker. Suppose that we use a spanned record organization, where each block has a 5-byte pointer to the next block (this space is not used…
arrow_forward
Guidelines
The input and the output of the program will be from & to files.
For the input file: o The program will take the file name as an argument (args [0]).
For the output file: o The program will take the output file name as an argument (args [1]).
You are free to explore the input and output methods as you develop the solution to the assignment; but before the submission, you have to prepare all the programs to take the input from a file in (args [0]) and print the output to the file in (args [1]).
Attached to this assignment are sample input and output files for each question. Those files have the same format as the files that your program will be tested through. So, please adhere to the format in those files, and make sure that your code can read and write the data given in that format.
arrow_forward
You are working on development of a new water treatment plant for Spokane, Washington. Monthly rainfall data has been collected as part of designing the new water treatment plant, and you are required to develop a computer program that will perform statistical analysis. The input data file is called rainfall. As you examine the input file, you notice that the first record line is the name of the city and type of data, the second record line contains the control numbers, and the remaining record lines are the average monthly rainfall amounts: 12 rows (January – December) and 6 years (2015 – 2020).
Write a C program that will read the data from the input file, such that the city name and type of data are stored in a one-dimensional character array, and the rainfall amounts are stored in a two-dimensional array. Manipulate the two-dimensional array to calculate the average rainfall for each year and the standard deviation in rainfall for each year. Then find the minimum and maximum…
arrow_forward
The header record of a batch file contains totals of items in the file. Each time the file is processed, the totals are also updated. Nightly, after the batch processes, the relevant data fields are summed and compared with the totals. Unbalanced conditions are reported and corrected. This example describes:
a) Segregation of duties
b) Application output control
c) Application edit check
d) Applicaiton input control
arrow_forward
what is the use of ''w'' in file handling ?
arrow_forward
Given the table below, declare the following variables with the corresponding data types and initialization values. Output to the screen the variable names together with the values.
arrow_forward
C++
Assignment Setup
Part 1: Working With Process IDs
Modify the getProcessID() function in the file named Processes.cpp
The function must find and store the process's own process id
The function must return the process id to the calling program. Note that the function currently returns a default value of -1.Hint: search for “process id” in the “System Calls” section of the Linux manual.
Part 2: Working With Multiple Processes
Modify the createNewProcess() function in the file named Processes.cpp as follows:
The child process must print the message I am a child process!
The child process must then return a string with the message I am bored of my parent, switching programs now
The parent process must print the message I just became a parent!
The parent process must then wait until the child process terminates, at which point it must return a string with the message My child process just terminated! and then terminate itself. Hint: search for “wait for process” in…
arrow_forward
This is the most effective file organization method in which one must handle all data records in a file named
arrow_forward
Flowchart needed not pseudocode
arrow_forward
Create the code that will allow you to define a variable that will be used to read data from a sequential access file. inFile is the name of the variable.
arrow_forward
EXPERIMENT 1
Create a script file (.m file) and name it as EXPT1_Surname
Your code shall get the input from an excel file named as Data Set which can be seen in the Experiment 1 Submission
Bin. It contains 100 data entries. You may directly import the excel file in MATLAB/MATLAB mobile or create a
table/array containing the 100 data entries. You may already set the input and not prompt the user to type the
values manually anymore.
After running the code, the user is asked to type the number corresponding to the desired output to be shown on
the command window. You may use IF-ELSE or SWITCH-CASE statements.
Type 1 for all measures of central tendencies in table form
Type 2 for all types of means (arithmetic, geometric, and harmonic) in table form
Type 3 for all measures of variation in table form
Type 4 for all results in table form
If the user types a number that is not within 1-4, an output stating "ERROR, KINDLY RUN THE CODE AGAIN AND TYPE
A NUMBER BETWEEN 1 AND 4" should be shown.
arrow_forward
This function is used to create an user accounts dictionary and another login dictionary. The given argument is the filename to load. Every line in the file should be in the following format: username - password The key is a username and the value is a password. If the username and password fulfills the requirements, add the username and password into the user accounts dictionary. To make sure that the password fulfills these requirements, be sure to use the signup function that you wrote above.
For the login dictionary, the key is the username, and its value indicates whether the user is logged in, or not. Initially, all users are not logged in.
What you will do: - Create an empty user accounts dictionary and an empty login dictionary. - Read in the file. - If the username and password fulfills the requirements, adds the username and password into the user accounts dictionary, and updates the login dictionary. - You should also handle…
arrow_forward
File Attributes
5. File attributes
(a) File name: file name is a very important file attribute When a file changes name, it seems to change its
identity (just like when you change your name)
The oldest file naming we may remember is the 8 +3 convention in DOS (in DOS, all file names can NOT be
bigger than 8 characters, special characters like spaces were not allowed etc.). So a file like C:\\Program
Files\\Adobe\\Photoshop can cause problems in DOS, and it has to be represented in different ways using strange
character like~ (tilde).
Elaborate on your understanding of file names or file naming in different operating systems (Windows, Mac,
Linux etc.). For example, some OS like NTFS may allow longer file name, allow space, and even allow non
English like characters such as Chinese file names. Is there a maximum length of file names (can you have a file
name of 1,000 characters long?)
(b) File creation date and modification date. File can be created and modified using different ways of…
arrow_forward
Use python please
arrow_forward
The three most important file structure approaches should be described.
arrow_forward
Objective:
Practice file handling
Lab Description:
Write a python script (with comments) that computes the average score for an unknown number of students in a
file. Specifically, your script should:
Ask for a filename name (that has scores in it)
Open the scores file
Verify the scores file opened correctly
Read the student's name and their four scores
Display the student's name, all of their scores and their average to the screen
Writes the students name and the scores' average to a file name "averages.csv"
Close both files
For example, the following are examples of correct execution (with the text in bold being the input from the user):
Input file
project4-inputA.txt: Output to the screen:
Output file
"averages.csv"
Мary
76
89
82
100
Joey
Please enter the scores filename: project4-inputA.txt # Student name, grade average
You entered project4-inputA.txt
Opened scores file project4-inputA.txt
Mary scores: 76.0 89.0 82.0 100.0 average: 86.75
Joey scores: 91.0 81.0 83.0 95.0 average:…
arrow_forward
Create a brief code segment that moves the file pointer 50 bytes from the file's start. Assume the file is already open and BX has the file handle.'
arrow_forward
Question
The Gregorian calendar is commonly used to mark the passing of time, however, it is not the only possibility. The World calendar is one alternative.
Features of the World calendar are as follow:
There are 12 months: January, February, March, April, May, June, July, August, September, October, November, December.
January, April, July and October have 31 days, all the rest have 30 days.
There are 7 days in every week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.
A date always occurs on the same day of the week regardless of year.
The year starts on Sunday 1st January.
The last day of the year is World day. It occurs between December of the current year and January of the following year.
Every leap year, a Leapyear day occurs between June and July.
(A leap year is a year that is divisible by 4 and not 100, unless it is divisible by 400.)
Example World calendar dates: 1st January 2023, World day 2022, 23rd March 1919, Leapyear day 2024.
A date can expressed as a…
arrow_forward
Note: Write program in c language format use in program (Printf & Scanf)
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Related Questions
- Question No: 2012123nt505 2This is a subjective question, hence you have to write your answer in the Text-Field given below. 76610 A team of engineers is designing a bridge to span the Podunk River. As part of the design process, the local flooding data must be analyzed. The following information on each storm that has been recorded in the last 40 years is stored in a file: the location of the source of the data, the amount of rainfall (in inches), and the duration of the storm (in hours), in that order. For example, the file might look like this: 321 2.4 1.5 111 33 12.1 etc. a. Create a data file. b. Write the first part of the program: design a data structure to store the storm data from the file, and also the intensity of each storm. The intensity is the rainfall amount divided by the duration. c. Write a function to read the data from the file (use load), copy from the matrix into a vector of structs, and then calculate the intensities. (2+3+3)arrow_forwardTopics: Functions, Files Read and write, DictionarySuppose you are given an input file of a document that only contains English words, spaces, commas(always followed with one space) and periods (always followed with one space). Your task is to readthe file, count the word frequency by ignoring the letter case, output the frequently used words (i.e.,the words occurred more than once) and the most frequently used word among all of them.Lab Scenario: Count the word in a document1. The program reads from an already provided input file: “document.txt”, which containsseveral paragraphs separated by an empty line.2. You will perform the file open operation. And then perform the read operation with your choice ofread functions and read the content of the file.3. Once you are done reading, start processing the contents of the file using a dictionary where thekey would be the word in lowercase and the corresponding values would be word frequency whichwill be the number of occurrences in the…arrow_forwardIntroduction:It is often necessary to reformat data in files. Sometimes it is done for readability, sometimes it is done to fit as input to different programs. Purpose:The purpose of the task is to provide proficiency in simple text management and file management.The task is the basis for file management. Reading instructions:You should be done with all the steps up to and including file management. Implementation: You will create a standalone program that performs the tasks below. Start by solving task one and complete that task before starting task 2. Your program should read the files mentioned from the "current working directory".All files are in text format with "\ n" at the end of the line. If you wish, you can use pipes and redirects to solve the file reading and printing instead of std :: ifstream and std :: ofstream.Comment on it in the code along with examples of how the program is used. The file names.txt contains names and social security numbers in the following form. The…arrow_forward
- The listing file is identified by a) source file name b) extension .LSF c) source file name and an extension .LSF d) source file name and an extension .LSTarrow_forwardPythonarrow_forwardC# please Write a program that will create file with at least 10 lines. Each line of your file should have information delimited by some delimiter (I.e., commas, spaces, slashes, symbols, etc.). After your file has been created with the proper amount of data, you should read your file line by line and display without the delimiters in the same program after closing your writer. Additionally, the information stored into your file should be data that is gathered from a separate class. Your program should save your file in a specific directory. To promote flexibility, your program should check to see if the chosen directory exists prior creating it, and if it does not exist, your program should create it for you. Additionally, you should allow the user to enter the names of the directory and file instead of hard-coding it into your program.arrow_forward
- I just need help forming the source code! any help would be great! You are working on development of a new water treatment plant for Spokane, Washington. Monthly rainfall data has been collected as part of designing the new water treatment plant, and you are required to develop a computer program that will perform statistical analysis. The input data file is called rainfall. As you examine the input file, you notice that the first record line is the name of the city and type of data, the second record line contains the control numbers, and the remaining record lines are the average monthly rainfall amounts: 12 rows (January – December) and 6 years (2015 – 2020). Write a C program that will read the data from the input file, such that the city name and type of data are stored in a one-dimensional character array, and the rainfall amounts are stored in a two-dimensional array. Manipulate the two-dimensional array to calculate the average rainfall for each year and the standard deviation…arrow_forwardA file has r = 20, 000 STUDENT records of fixed length. Each record has the following fields: NAME (30 bytes), SSN (9 bytes), ADRESS(40 bytes), PHONE(9 bytes), BIRTHDATE (8 bytes), SEX(1 byte), CLASSCODE( 4 bytes, integer) MAJORDEPTCODE(4 bytes), MINORDEPTCODE(4 bytes), and DEGREEPROGRAM( 3 bytes). An additional byte is used as a deletion marker. Block size B = 512 bytes. a) Calculate the blocking factor bfr (=floor(B/R), where R is the record size) and number of file blocks b, assuming unspanned organization (a record can’t be split across blocks). b) Suppose only 80% of the STUDENT records have a value for PHONE, 85% for MAJORDEPTCODE, 15% for MINORDEPTCODE, and 90% for DEGREEPROGRAM. We use a variable-length record file. Each record has a 1-byte field type for each field in the record, plus the 1-byte deletion marker and a 1-byte end-of-record marker. Suppose that we use a spanned record organization, where each block has a 5-byte pointer to the next block (this space is not used…arrow_forwardGuidelines The input and the output of the program will be from & to files. For the input file: o The program will take the file name as an argument (args [0]). For the output file: o The program will take the output file name as an argument (args [1]). You are free to explore the input and output methods as you develop the solution to the assignment; but before the submission, you have to prepare all the programs to take the input from a file in (args [0]) and print the output to the file in (args [1]). Attached to this assignment are sample input and output files for each question. Those files have the same format as the files that your program will be tested through. So, please adhere to the format in those files, and make sure that your code can read and write the data given in that format.arrow_forward
- You are working on development of a new water treatment plant for Spokane, Washington. Monthly rainfall data has been collected as part of designing the new water treatment plant, and you are required to develop a computer program that will perform statistical analysis. The input data file is called rainfall. As you examine the input file, you notice that the first record line is the name of the city and type of data, the second record line contains the control numbers, and the remaining record lines are the average monthly rainfall amounts: 12 rows (January – December) and 6 years (2015 – 2020). Write a C program that will read the data from the input file, such that the city name and type of data are stored in a one-dimensional character array, and the rainfall amounts are stored in a two-dimensional array. Manipulate the two-dimensional array to calculate the average rainfall for each year and the standard deviation in rainfall for each year. Then find the minimum and maximum…arrow_forwardThe header record of a batch file contains totals of items in the file. Each time the file is processed, the totals are also updated. Nightly, after the batch processes, the relevant data fields are summed and compared with the totals. Unbalanced conditions are reported and corrected. This example describes: a) Segregation of duties b) Application output control c) Application edit check d) Applicaiton input controlarrow_forwardwhat is the use of ''w'' in file handling ?arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,