C/C++ Lessons [Lesson 01]
Well, I'm taking C Programming this semester, so I thought I'd share some of the questions we have to do on a weekly basis. Although these were designed to be done in C, I'm aware that most people in the C-discipline use C++ nowdays, my Uni is just determined to keep it old school.
I'll start from Week 1 and gradually release more every week, posting the previous week's answers as I go, also awarding marks to people who complete the Lab. I will give each Lab a mark out of 10, completion and code efficiency are both needed to obtain full marks. Unless specifically told you can use them, function like sprintf are frowned upon because they just bypass any learning, although you can use existing functions to do these questions quickly (in some cases at least) they are really designed to be done manually if you want to maximize your learning.
No, this isn't so that I get people to do my homework for me, I'm already 3 weeks ahead of the course.
Okay, here is the key:
Questions with no
== Expected to be able to do with no problems.
== intermediate/beginner questions, optional but good for furthering your knowledge.
== intermediate questions, designed for people with prior coding experience.
== intermediate/advanced questions (obviously that's a relative term, advanced in relation to the course)
Now, the questions:
LABSHEET 1 WEEK 1
DESCRIPTION:
ROT.C SOURCE CODE:
QUESTIONS:
1. Modify the program to perform rot-5, rotation by 5 characters (or Caesar-encoding).
2. Extend the program to print out each character of the ciphered text one per line instead of all the characters on one line.
3. Extend the program to print out on each line not only the ciphered character, but also the original character too.
4. Extend the program to print out on each line not only the original and ciphered characters, but also the "position" of the character in the text.
5. Extend the program to handle both upper-case and lower-case letters.
END OF REQUIRED QUESTIONS
6.
Extend the program so that it can accept multiple arguments, performing the ciphering process on each argument to the program.
7.
Extend the program so that it reads the rotation amount (the amount to rotate each character by) as the first argument to the program. You will need to use the atoi function to convert the text argument into a number.
8.
Extend the program so that all arguments to the program can be either text or numbers. If an argument is text, the text is ciphered using the current rotation amount (initialised to 13). If an argument is a number, the rotation amount changes to this value and all subsequent cipherings (up to the next number argument) use this new rotation amount.
9.
Extend the previous version of the program so that textual arguments can represent files. If a text argument can be opened as a file (see man fopen), perform the ciphering on the contents of the file. Otherwise, treat the arguments as before: if an argument is text, the text is ciphered using the current rotation amount (initialised to 13), or if an argument is a number, the rotation amount changes to this value and all subsequent cipherings (up to the next number argument) use this new rotation amount.
Okay, send your answers in to me, but feel free to ask questions you may have in this thread.
Good Luck and I hope you learn something.
-Jason.
I'll start from Week 1 and gradually release more every week, posting the previous week's answers as I go, also awarding marks to people who complete the Lab. I will give each Lab a mark out of 10, completion and code efficiency are both needed to obtain full marks. Unless specifically told you can use them, function like sprintf are frowned upon because they just bypass any learning, although you can use existing functions to do these questions quickly (in some cases at least) they are really designed to be done manually if you want to maximize your learning.
No, this isn't so that I get people to do my homework for me, I'm already 3 weeks ahead of the course.
Okay, here is the key:
Questions with no
== Expected to be able to do with no problems.
== intermediate/beginner questions, optional but good for furthering your knowledge.
== intermediate questions, designed for people with prior coding experience.
== intermediate/advanced questions (obviously that's a relative term, advanced in relation to the course)Now, the questions:
LABSHEET 1 WEEK 1
DESCRIPTION:
Code:
You are given a file named "rot.c". This file performs ROT-13 encoding on a command line argument. You will need to research ROT encoding (very basic encoding) and then modify the program as specified in the questions. Before you begin coding take some time to look at what the existing code does. Good Luck. -Jason
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define ROT 13
/* Post: rotate c returns the character ROT positions further along the
alphabetic character sequence from c, or c if c is not lower-case
*/
static char rotate(char c)
{
/* Check if c is lower-case or not */
if (islower(c))
{
/* The ciphered character is ROT positions beyond c,
allowing for wrap-around
*/
return ('a' + (c - 'a' + ROT) % 26);
}
else
{
return c;
}
}
int main(int argc, char *argv[])
{
/* Exit with an error if the number of arguments (including
the name of the executable) is not precisely 2
*/
if(argc != 2)
{
fprintf(stderr, "%s: program expected 1 argument, but instead received %d\n", argv[0], argc-1);
exit(EXIT_FAILURE);
}
else
{
/* Calculate the length of the first argument */
int length = strlen(argv[1]);
/* Loop for every character in the text */
for (int i = 0; i < length; i++)
{
/* Determine and print the ciphered character */
printf("%c", rotate(argv[1][i]));
}
/* Print one final new-line character */
printf("\n");
/* Exit indicating success */
exit(EXIT_SUCCESS);
}
return 0;
}
1. Modify the program to perform rot-5, rotation by 5 characters (or Caesar-encoding).
2. Extend the program to print out each character of the ciphered text one per line instead of all the characters on one line.
3. Extend the program to print out on each line not only the ciphered character, but also the original character too.
4. Extend the program to print out on each line not only the original and ciphered characters, but also the "position" of the character in the text.
5. Extend the program to handle both upper-case and lower-case letters.
END OF REQUIRED QUESTIONS
6.
Extend the program so that it can accept multiple arguments, performing the ciphering process on each argument to the program.7.
Extend the program so that it reads the rotation amount (the amount to rotate each character by) as the first argument to the program. You will need to use the atoi function to convert the text argument into a number.8.

Extend the program so that all arguments to the program can be either text or numbers. If an argument is text, the text is ciphered using the current rotation amount (initialised to 13). If an argument is a number, the rotation amount changes to this value and all subsequent cipherings (up to the next number argument) use this new rotation amount.9.

Extend the previous version of the program so that textual arguments can represent files. If a text argument can be opened as a file (see man fopen), perform the ciphering on the contents of the file. Otherwise, treat the arguments as before: if an argument is text, the text is ciphered using the current rotation amount (initialised to 13), or if an argument is a number, the rotation amount changes to this value and all subsequent cipherings (up to the next number argument) use this new rotation amount.Okay, send your answers in to me, but feel free to ask questions you may have in this thread.
Good Luck and I hope you learn something.
-Jason.

(on the forum) mainly when they have a code to follow in C
, I'm just saying that seeing as I'm not actually teaching the C theory, people can make their own choice As to what language they use. Of course I'd encourage people to do it in C, but few people on these forums use C, which is why I said that if people want, they can solve these in C++. I didn't say my class at uni was using C++, I said that most of the general programming population use C++ instead of straight C these days, we do the labs using C, but seeing as people here aren't restricted by my curriculum, they can use c++ if they feel more comfortable with it.