You are not logged in.
Hey guys,
I've got my final exam coming up this Monday. I got the final exams of the last couple of years from my faculty's print-press. I thought I'd share some of them with you, maybe someone would enjoy solving them. I haven't tried to solve them yet, I'll be doing that over the next few hours. I'll provide the answers to the exercises if I were able to solve them.
1. Give a C function reverse that takes an integer array and its length as arguments and reverses the array in place. Show how to use this function in a main program. Your program should accept the size of the array and the list of the array. Maximum size of array is 20.
2. Write a program that accepts two 5x5 matrices, swap their diagonals, and then print both matrices. The program should have two functions one that accepts 5x5 matrix and the other print 5x5 matrix.
3. Write a program that inputs a line of string up to 50 characters. After reading the string, the program should count all punctuations and print results. You need to create a pointer that scans the string to look for punctuation. Also, use the built-in function in ctype.h to check for punctuation. *
4. Write a C program that accepts two strings up to 15 characters each. The program should compare both strings and concatenate them with smaller first. The final string should be saved in a declared 30 character string and printed out. The program should also print the size of the final string. *
* These are not required for my exam because we couldn't take strings in C because of several strikes and vacations.
Enjoy :)
P.S: I know AymanFarhat would enjoy this post ;)
Last edited by Kassem (June 18 2010)
1.
int func (int *array,int length) {
int i;
for (i=0;i<(length/2);i++) {
array[i] ^= array[length-1-i];
array[length-1-i] ^= array[i];
array[i] ^= array[length-1-i];
}
}
Code is not portable. There is few other tricky ways possible...
For those wondering about those xor operations:
[a, b]
[a xor b, b]
[a xor b, b xor a xor b] = [a xor b, a]
[a xor b xor a, a] = [b, a]
Hence the above swaps a and b. xor is the exclusive or operation. To me this is hackery, or microprocessor instruction set baserd optimization/abuse.
Here's for me the simplest way to swap variables :
int a, b;
//Give them a value;
a=a+b;
b=a-b;
a=a-b;
Main advantage, you do not decalre a third (useless) variable. Default: hard(er) to understand for future maintainers.
@Kassem:
Nice exercises. Post your questions if you need help ;)
And about strings, you should do the exercises nontheless. A string is an array of 'char' as easy as this. However it has a final char appended to it : '\0'. So 'Kassem' would be an array :
'K' 'A' 'S' 'S' 'E' 'M' '\0' which is a 7 entries array.
So here you go, do the exercises. One day if I have time I'll argue why a good knowledge of C makes a difference between a good and a bad programmer ;)
Last edited by Joe (June 18 2010)
Good luck in your exams and God bless you guys.
Hey guys,
I've got my final exam coming up this Monday. I got the final exams of the last couple of years from my faculty's print-press. I thought I'd share some of them with you, maybe someone would enjoy solving them. I haven't tried to solve them yet, I'll be doing that over the next few hours. I'll provide the answers to the exercises if I were able to solve them.
1. Give a C function reverse that takes an integer array and its length as arguments and reverses the array in place. Show how to use this function in a main program. Your program should accept the size of the array and the list of the array. Maximum size of array is 20.
2. Write a program that accepts two 5x5 matrices, swap their diagonals, and then print both matrices. The program should have two functions one that accepts 5x5 matrix and the other print 5x5 matrix.
3. Write a program that inputs a line of string up to 50 characters. After reading the string, the program should count all punctuations and print results. You need to create a pointer that scans the string to look for punctuation. Also, use the built-in function in ctype.h to check for punctuation. *
4. Write a C program that accepts two strings up to 15 characters each. The program should compare both strings and concatenate them with smaller first. The final string should be saved in a declared 30 character string and printed out. The program should also print the size of the final string. *
* These are not required for my exam because we couldn't take strings in C because of several strikes and vacations.
Enjoy :)
P.S: I know AymanFarhat would enjoy this post ;)
Awesome, thanks for sharing, and good luck in your exams :)
Edit: I agree with rahmu, as C is an excellent language to learn for any programmer as it gives a good base plus it forces the programmer to learn to make robust programs and take good good care about everything he does in his program. In addition, with having less already written libraries unlike Java, it is ideal for making the student write everything on his own rather than rely on APIs. Kudos for the Lebanese University and it's curriculum :)
So IMO even if one will never use C in the future, knowledge in it still can be beneficial.
Last edited by Ayman (June 18 2010)
Here's an article that gives the best definition of Java vs C in programming terms.
I strongly recommend reading the 'The Pitfalls of Java as a First Programming Language' paragraph. The author argues that Java programmers act like 'plumbers in a hardware store' looking for the tool that does the job best, instead of learning how to solve the job themselves. The problem at hand (string management in C) shows best this point.
Most languages, especially the Object Oriented ones, will give you a abstraction layer, a plumber tool, to do the job best. Most probably a wrapper class. C have you get your hands dirty and deal directly with low-level management. From a pedagogical point of view, this is my preferred approach.
Which in 99.99% of the time, you won't need.
It should be noted as such.
YES.
But the 0.01% of times where you actually need it, it'll come pretty handy. This is when you'll know the difference between a programmer, and a guy who simply uses tools.
1.
int func (int *array,int length) { int i; for (i=0;i<(length/2);i++) { array[i] ^= array[length-1-i]; array[length-1-i] ^= array[i]; array[i] ^= array[length-1-i]; } }
Code is not portable. There is few other tricky ways possible...
Is there a simpler way?
Is there a simpler way?
Declare a new array, then get rid of the original one.
And about strings, you should do the exercises nontheless. A string is an array of 'char' as easy as this. However it has a final char appended to it : '\0'. So 'Kassem' would be an array :
'K' 'A' 'S' 'S' 'E' 'M' '\0' which is a 7 entries array.
Yup, I watched a few videos last night. The tutor made it clear that there's nothing called "string" in C. It's just an array of chars that can be manipulated using pointers.
Kassem wrote:Is there a simpler way?
Declare a new array, then get rid of the original one.
I tried doing that. But messed it up lol.
Last edited by Kassem (June 18 2010)
Post your code, I'll tell you what went wrong :)
C is quite complicated at first. It doesn't get easier after either.
But hey, think about it that way: this is the best practice you could be doing at this stage ;-)
I think I got it...
int reverse(int *arr, int size)
{
int temp[size];
int counter = 0;
for(int i = size; i >= 0; i--)
{
temp[i] = arr[counter];
counter++;
}
arr = temp; //am not sure this can be done in C...
}
Last edited by Kassem (June 18 2010)
You have one error in the limits of your for loop. In theory arr = temp could be done in C.
Best way to find out is to try it, no?
Also think of liberating old temp from memory. It won't really matter today, but it is a good reflex to have .
Last edited by Joe (June 18 2010)
Seriously! how in the hell do you pass in a 2 dimensional array into a function?! Shouldn't I use a pointer?
Arrays are pointers.
Think of how arrays are stored in memory, and you'll figure out how to do this :)
You have one error in the limits of your for loop. In theory arr = temp could be done in C.
Best way to find out is to try it, no?Also think of liberating old temp from memory. It won't really matter today, but it is a good reflex to have .
It compiled but then I got this error:
Run-Time Check Failure #2 - Stack around the variable 'temp' was corrupted.
Arrays are pointers.
Think of how arrays are stored in memory, and you'll figure out how to do this :)
So if I passed array[0][0] that should work? Ok I'll try it myself lol
rahmu wrote:You have one error in the limits of your for loop. In theory arr = temp could be done in C.
Best way to find out is to try it, no?Also think of liberating old temp from memory. It won't really matter today, but it is a good reflex to have .
It compiled but then I got this error:
Run-Time Check Failure #2 - Stack around the variable 'temp' was corrupted.
I told you to check your for loop !
Is temp[size] defined?
Is temp[size] defined?
Nope, I changed that to temp[5] :)
And I still cannot get anything to compile lol. I officially hate C now
#include <stdio.h>
void reverse(int *arr, int size);
void main()
{
int a[5] = {5,2,6,3,7};
reverse(a, 5);
for(int i = 0; i < 5; i++)
{
printf("%d\t",a[i]);
}
}
void reverse(int *arr, int size)
{
int temp[5];
int counter = 0;
for(int i = size - 1; i >= 0; i--)
{
temp[i] = arr[counter];
counter++;
}
arr = temp;
getchar();
}
oh c'mon! what's wrong with the above code now?! It compiles and everything but doesn't output anything!
Which in 99.99% of the time, you won't need.
It should be noted as such.
Depends where [as in which part of the industry] you are working.
Here's for me the simplest way to swap variables :
int a, b; //Give them a value; a=a+b; b=a-b; a=a-b;
Main advantage, you do not decalre a third (useless) variable. Default: hard(er) to understand for future maintainers.
@Kassem:
Nice exercises. Post your questions if you need help ;)
And about strings, you should do the exercises nontheless. A string is an array of 'char' as easy as this. However it has a final char appended to it : '\0'. So 'Kassem' would be an array :
'K' 'A' 'S' 'S' 'E' 'M' '\0' which is a 7 entries array.
So here you go, do the exercises. One day if I have time I'll argue why a good knowledge of C makes a difference between a good and a bad programmer ;)
rahmu - ur way will fail if a+b will overflow integer variable.
Last edited by nuclearcat (June 18 2010)