Here's a simple programming challenge for all of you. For each challenge, you get 5 points for writing code that does what it's supposed to. 1 - 4 points for code that doesn't work, depending on how close it it. In a few days I'll write my own code for each challenge, and you get five points if you code runs faster than mine and 5 points if your code uses less memory than mine
You can use either C or C++ and any of the STL.
I think it'll give you guys some good practice as well as learn from a pro.
Spoiler for Challenge 1:
You have a path to a file, the problem is that it is a windows path and you need to use it on a *Nix machine. Windows uses forwards slashes for paths, *nix uses backslashes. You need to write a method that will convert
"..\MyDocuments\Programming\Tut1.cpp"
into
"../MyDocuments/Programming/Tut1.cpp"
Code:
#include <stdio.h>
const char * ConvertToUNIX(const char * str){
/*
This method returns a char string, which is a UNIX encoded path
The parameter is the original string
Your conversion goes here
*/
}
int main(void){
/*
There are two slashes for each one because \ is a reserved character
which tells the compiler that a special character is coming after it, \
is the id of that special character, in the string it will act like one
character.
*/
const char * path = "..\\MyDocuments\\Programming\\Tut1.cpp";
printf("Modified string: %s\n", ConvertToUNIX(path));
}
Spoiler for Challenge 2:
Write a method that returns the sum of all values from one to and including n. For example, if you pass the method the number 5, it should return 10 (5 + 4 + 3 + 2 + 1)
Code:
#include <stdio.h>
unsigned int Sum(unsigned int Value){
//Your code here
}
int main(){
unsigned int TestValue = 10000;
printf("Original string: %d\n", Sum(TestValue));
}
Spoiler for Challenge 3:
Write a method that parses a simple string and analyzes it. The string will have one of the basic math operations (* - + /) and two numbers. ie "12+4". It will always be two numbers and one operation.
Code:
#include <stdio.h>
int Analyze(const char * math){
//Your code goes here
}
int main(void){
char * test = "12+4";
printf("Answer: %d\n", Analyze(test));
char * test2 = "4*100";
printf("Answer: %d\n", Analyze(test2));
}
Spoiler for Challenge 4:
Write a method that counts how many time a certain character appears in a string.
Code:
#include <stdio.h>
int Count(const char * str, char search){
//Your code goes here
}
int main(void){
char * test = "Hello World, I'm a lil string";
printf("Answer: %d\n", Count(test, 'l'));
}
Spoiler for Challenge 5:
Write a method that counts how many time a certain character appears in a string.
Code:
#include <stdio.h>
#define NumberOfNumbers 15
void Sort(int * array){
//Your code goes here
}
int main(void){
int test[]= {10, 4, 65, 12, 43, 53, 23, 6, 13, 54, 12, 43, 45, 12, 44};
Sort(test);
unsigned int i;
for (i = 0; i < NumberOfNumbers; i++){
printf("Number %d: %d\n", i, test[i]);
}
}
Bookmarks