100% Tested with Dartmouth x IMT Online Judge/Task Grader
Basic Syntax
With Pointers
Practice
You'd like to implement a date feature in the C programming language. To this end you are provided with a structure definition, a main function, and two function prototypes: "readDate()" and "printDate()". All that is left for you to do is to write these two functions.
Here are the exact specifications:
The function readDate() should read 3 integers from the user input. The first integer is the year (a 4-digit number), the second integer is the month, and the third integer is the day of the date being read. The function should store these three numbers in the appropriate parts of the structure being passed into it.
The function printDate() should print the date stored in the variable passed into it in the following format: mm/dd/yyyy with a new line afterwards. So the month should be printed with two digits (01, 02, 03, …, 11, 12), the day should be printed as two digits (01, 02, 03, …, 30, 31), and the year should be printed as a 4-digit number.
Input:
2018 10 2
Output:
10/02/2018
Solution
-> Used for manipulation with pointer
Example:
Accepting user input:
Structure Space/Memory
Min = sum of all component; sum compiler implement memory alignment
Manipulate Struct with Function
In this problem you will continue developing the data feature which you started implementing in the previous problem. You will implement a "tomorrow" feature in the C programming language via a function called "advanceDay()". The function advanceDay() should take as input a date (stored in a struct date) and return the date of the following day. You do not have to take into account leap years (although you may if you wish to). That is, it is okay for your function to always return March 1 as the day following February 28, no matter the year.
You are provided with a familiar date structure definition, a main function as well as the function prototypes for the readDate(), printDate(), and advanceDay() functions. Do not modify any of the given code. Simply add your function definitions underneath the main() function. For the readDate() and printDate() functions you may simply copy and paste the code you developed in the previous task.
Input:
2018 10 2
Output:
10/02/2018
10/03/2018
Input:
2018 10 31
Output:
10/31/2018
11/01/2018
Solution:
Triangle
Pointers
Polygon
Practice:
In this task, we will continue to work with polygons. You are provided with the following:
Please do not alter the provided code (except to fill in the main function and add your initializePoly() function).
initializePoly() should receive as input a pointer to the first structure in an array of structures as well as an integer, indicating how many points can be stored in the array. Your job is to initialize these points in the following way:
Using a for loop with i starting at 0, initialize the x-coordinate of the point at index i as -i, and the y-coordinate as i*i.
Your main function should read the number of vertices to store in the array of points from the user, then allocate the appropriate amount of memory, initialize the array with the function initializePoly, and finally print the vertices of the polygon using the function printPoly().
Input:
4
Output:
(0, 0)
(-1, 1)
(-2, 4)
(-3, 9)
Solution
Problem:
You would like to store student data (for each student, their name and age) in a linked list of students. You are given the following structure to store each student's information. Please do not modify this structure:
Your first task is to write a function createStudent() that takes as inputs a string (namely a student's name) and an integer (their age) and that returns a pointer to a struct student which stores this information. Your function should dynamically allocate the memory required for this struct student and then write the student's name and age into this newly allocated memory.
You are given the createStudent() function prototype and a main() function to test your code, please do not modify these:
Input:
Petra 25
Output:
New student created: Petra is 25 years old.
Input:
Remi 18
Output:
New student created: Remi is 18 years old.
Solution:
Problem:
In this task you will continue working on the linked list of students in which you would like to store, for each student, their name and age. As before you are provided with some code that you should not modify:
A structure definition for the storage of each student's information.
A main() function to test your code.
Prototypes for the functions createStudent() (from the previous task) and append() (from the current task).
You will need the function definition (from the previous task) for createStudent(), as well as any other functions you added, such as copyStr() for example. If you were unable to solve the previous task you have the option to be given the code for the createStudent() function (see the quiz preceding this task) so that you can work on the current task.
Your new task is to write a function append() which takes as input two pointers: the first pointer holds the address of the current end of the linked list of students, the second pointer points to a newly created student. Your function should append this new student to the linked list and return the address (a struct student *) of the new end of the list.
Input:
25 18 32
Output:
Petra is 25 years old.
Remi is 18 years old.
Mike is 32 years old
Solution:
Problem:
In this task you will continue working on the linked list of students in which you would like to store, for each student, their name and age. As before you are provided with some code that you should not modify:
A structure definition for the storage of each student's information.
A main() function to test your code.
Prototypes for the functions createStudent(), append() (from previous tasks) and printStudents() (from the current task).
You will need the function definitions (from previous tasks) for createStudent(), append() as well as any other functions you added, such as copyStr() for example. If you were unable to solve the previous task you have the option to be given the code for the append() function (see the quiz preceding this task) so that you can work on the current task.
Your new task is to write a function printStudents() which takes as input a pointer that holds the address of the start of a linked list of students. Your function should then print this list of students to the screen as specified in the example below. Your function should not return anything.
Input:
25 18 32
Output:
Petra is 25 years old.
Remi is 18 years old.
Mike is 32 years old.
Solution:
In this task you will continue working on the linked list of students in which you would like to store, for each student, their name and age. As before you are provided with some code that you should not modify:
A structure definition for the storage of each student's information.
A main() function to test your code.
Prototypes for the functions createStudent(), append(), printStudents (from previous tasks) and freeStudents() (from the current task).
You will need the function definitions (from previous tasks) for createStudent(), append(), printStudents() as well as any other functions you added, such as copyStr() for example. If you were unable to solve the previous task you have the option to be given the code for the printStudents() function (see the quiz preceding this task) so that you can work on the current task.
Your current task is to write a function freeStudents() which takes as input a pointer that holds the address of the start of a linked list of students. Your function should then free the space allocated for each student in this list of students. Your function should not return anything.
Input:
25 18 32
Output:
Petra is 25 years old.
Remi is 18 years old.
Mike is 32 years old.
Solution:
Problem:
In this task you will work with the linked list of digits we have created in the lessons up to this point. As before you are provided with some code that you should not modify:
A structure definition for the storage of each digit's information.
A main() function to test your code.
The functions createDigit(), append(), printNumber(), freeNumber() and readNumber() which we have written in the lectures.
Your task is to write a new function divisibleByThree() which takes as input a pointer that holds the address of the start of a linked list of digits. Your function should then check whether the number stored in this linked list of digits is divisible by three. The function should return the value 1 if indeed the number is divisible by three and it should return the value 0 otherwise.
Input:
234
Output:
The number 234
is divisible by 3.
Input:
74658
Output:
The number 74658
is divisible by 3.
Input:
245
Output:
The number 245
is not divisible by 3.
Solution:
Problem
In this task you will work with the linked list of digits we have created in the lessons up to this point. As before you are provided with some code that you should not modify:
A structure definition for the storage of each digit's information.
A main() function to test your code.
The functions createDigit(), append(), printNumber(), freeNumber(), readNumber() and divisibleByThree() (although you may not need to use all of these).
Your task is to write a new function changeThrees() which takes as input a pointer that holds the address of the start of a linked list of digits. Your function should change all of those digits in this linked list that equal 3 to the digit 9, and count how many replacements were made. The function should return this number of replacements.
Input:
234345632
Output:
The number 234345632
was modified in 3 places.
The new number is 294945692
Input:
4393293
Output:
The number 4393293
was modified in 3 places.
The new number is 4999299
Solution:
Example:
5 | 1 | 3 | Action |
---|---|---|---|
1 | 5 | x | front |
1 | 3 | 5 | break |
Problem:
In this task you will again work with the linked list of digits we created during the lessons up to this point.
You are provided with (but are not required to use) the functions and prototypes we have written so far. You are also provided with a main() function to test your code. Please do not modify this main() function.
Your task is to write a new function countRedun() which takes as input a pointer that holds the address of the start of a linked list of digits. Your function should count how many redundancies can be observed in the number stored in this list and return this count of redundancies. A redundancy is a digit which has previously already occurred in the number. For example, in the number 39534, the second '3' is a redundancy.
Input:
5243
Output:
The number 5243
contains 0 redundancies.
Input:
5256202
Output:
The number 5256202
contains 3 redundancies.
Solution:
Problem:
In this project you will work with the linked list of digits we have created in the lessons during this course. As before you are provided with some code that you should not modify:
A structure definition for the storage of each digit's information.
A main() function to test your code.
The functions createDigit(), append(), printNumber(), freeNumber() and readNumber() which we have written in the lectures.
Your task is to write a new function reverseNumber() which receives as input a pointer that holds the address of the start of a linked list of digits and that returns a pointer that holds the address of the start of a new linked list of digits, namely the original list but in reverse order.
Hint: You should sit down with a piece of paper and a pen first to design the steps necessary to solve this problem. What additional functions might be useful? Do not start coding right away. For example, it may help to write function which finds the length of a linked list of digits. Another useful function might be one which finds the nth digit in a linked list of digits. Think this through first before starting to code.
Examples:
Input
35267
Output
The reverse of 35267
is 76253
Input
5
Output
The reverse of 5
is 5
Solution: