C/C++ Lessons [Lesson 01]

Posts 1–15 of 15 · Page 1 of 1
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:
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
ROT.C SOURCE CODE:
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;
}
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.
Its not really about keeping it old school, but forcing you to do the harder way imo. (creating your own lists, using arrays and pointers more often, etc)
And if half the class is doing it in C++ they are doing it wrong lulz :X
Wut? Noone in the class is doing it in C++, I just said that if people here want to use C++ to answer the questions, that's fine by me.
Quote Originally Posted by Jason View Post
Wut? Noone in the class is doing it in C++, I just said that if people here want to use C++ to answer the questions, that's fine by me.
you said that half the people there uses c++. I thought that you were talking about the class it self.

and using c++ to answer the questions is not the correct way (on the forum) mainly when they have a code to follow in C
Quote Originally Posted by 'Bruno View Post
you said that half the people there uses c++. I thought that you were talking about the class it self.

and using c++ to answer the questions is not the correct way (on the forum) mainly when they have a code to follow in C
Where did I say half the people use C++? I don't really give a shit what language people use for this, I'm doing it in C, but if people want to translate rot.c into C++ and then answer the questions in C++ I said that's fine.
Quote Originally Posted by Jason View Post
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.
There, and chill dude, I'm not accusing anything to anyone. Just typing my opinion on this matter. And no they shouldn't be doing it in C++ on C "lessons". It has differences and some of them matter. Maybe not on this one, but doing new or malloc on a pointer has a difference while coding. (example)

edit: half or most... maybe i got what you said wrong.
What is this? Jason be in C++ teaching Brinuz.
Quote Originally Posted by why06 View Post
What is this? Jason be in C++ teaching Brinuz.
shit happens ;D
I'm chilled Bruno , 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.

@why06, first off welcome back Shaun!! Also, I'm not so much "teaching" C as I am just sharing my university lab sheets for others to try.
I know. How are u guys. it's been a few months since I been back.
Good good, it's been pretty boring in the mpgh programming section tho haha.
Quote Originally Posted by Jason View Post
Good good, it's been pretty boring in the mpgh programming section tho haha.
Wow. I'm so rusty. I haven't coded in months. I wonder if I can even still do this... :l

I need to take a class too. SO can have a fancy degree to wave around.
Lol I post on mpgh like every once in a week. xO And im quite like you, didnt code in the last 2 months.. meh
/Unstickied. You plan to make Lesson 02 ?
@Jason
Lol I got one response, so no haha.
Posts 1–15 of 15 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us