Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C (VIII)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
0xB16B00B5
Posts: 10
Joined: Tue Jul 24, 2012 6:11 am

Re: [Tutorial] Introduction to programming using C (VIII)

Post by 0xB16B00B5 » Mon Aug 06, 2012 9:04 pm

Completing the others.
Write a program that given 2 ASCII stings as an input, checks if they are exactly equal.
[spoiler]

Code: Select all

#include <stdio.h>

#define STRING1 "equal"
#define STRING2 "Equal"

int main()
{

	char* a = STRING1;
	char* b = STRING2;

	if (a == b)
	{
		printf("OK\n");
	}
	else
	{
	printf("Very bad\n");
	}

   return 0;
}
[/spoiler]
Write a program that checks if one string is part of another (if it's a substring).
[spoiler]

Code: Select all

#include <stdio.h>

#define SIZE  100
#define STRING1 "this is a string"
#define STRING2 "this is a"

int main()
{

	char a[SIZE] = STRING1;
	char b[SIZE] = STRING2;
	int x, y;

	y = strlen(STRING1);

	while (a[x] == b[x])
	{
		if (b[x] == '\0')
		{
			break;
		}
		else
		{
			x++;
		}			
	}

		printf("%d-character long substring, out of %d-character long main string\n", x, y);
	
   return 0;
}
[/spoiler]
I think I'm missing something here.
BTW, when I compiled it with terminal, I got this:
c.c:14:6: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
It ran fine though.
Write a program that given an array of 10 numbers, it orders it ascendantly.
I remember doing this before, but I get it scr..ed now. I'll try this again later.
Write a program that given 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive (that is, given "hello" and "HeLLo", it should say they are equal) This one could be a bit tricky ;)
[spoiler]

Code: Select all

#include <stdio.h>

#define STRING1 "STRING"
#define STRING2 "string"

int main()
{
	char s1[] = STRING1;
	char s2[] = STRING2;
	int l = 0;
	int equal;	
	
	for (l = 0; l <= 5; l++)
	{
		if (s1[l] > 64 && s1[l] < 91)
		{
			s1[l]+=32;
		}
		if (s2[l] > 64 && s2[l] < 91)
		{
			s2[l]+=32;
		}
	}

	while (s1[l] == s2[l])
	{
		if (l <= 5)
		{
			l++;
		}
		else
		{
			equal = 1;
			break;
		}
	}

	if (equal = 1)
	{
	printf("The strings are equal, but are case-different.");
	}
	else
	printf("The strings are unequal.")

   return 0;
}
[/spoiler]
A few notes about this one..
  • - I looked at snailface's code, but I took what I needed and rewrote it in my own way. Thanks for the unintended help.
    - I didn't want to do a blind copy & paste, so I inspected it deeper for a bit. At first I didn't understand what the "> 64" and "<91" meant, but I then remembered you said computers work by numbers, something with ASCII. There are 26 ABC letters, so inbetween 64 and 91 is probably the range of numeric values for capital letters. Adding 32 is supposed to change them into small letters. A little search with google and this table confirmed it. Yeah, I'm a hard working student.
Write a program that given two matrices float a[3][3] and float b[3][3], adds them.
[spoiler]

Code: Select all

#include <stdio.h>

int main()
{

	float a[3][3] = {4, 2, 7, 1, 7, 2, 5, 6, 2};
	float b[3][3] = {0, 9, 5, 3, 6, 8, 1, 4, 3};
	int x, y = 0;


	for (x = 0; x <= 2; x++)
	{
		for (y = 0; y <= 2; y++)
		{
			printf("[%1.0f + %1.0f] = [%1.0f]\n", a[x][y], b[x][y], a[x][y]+b[x][y]);
		}
	}
   return 0;
}
[/spoiler]
Not sure if that's what you meant.. here's one with an output similar to how it looks on WIkipedia:
[spoiler]

Code: Select all

#include <stdio.h>

int main()
{

	float a[3][3] = {4, 2, 7, 1, 7, 2, 5, 6, 2};
	float b[3][3] = {0, 9, 5, 3, 6, 8, 1, 4, 3};
	int x, y, temp1, temp2;


	for (x = 0; x <= 2; x++)
	{
		for (y = 0; y <= 1; y++)
		{
			temp2 = a[x][y] + b[x][y];
			printf("[%1.0f %1.0f] = [%1.0f %1.0f] = [%1.0f + %1.0f %1.0f + %1.0f] = [%1.0f %1.0f]\n", a[x][y], a[x][y+1], b[x][y], b[x][y+1], a[x][y], b[x][y], a[x][y+1], b[x][y+1], a[x][y]+b[x][y], a[x][y+1]+b[x][y+1]);

		}
		if (x <= 1)
		{
			printf("\n");
		}
	}
   return 0;
}
[/spoiler]
That printf line looks like a total mess imo.
Advertising

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VIII)

Post by m0skit0 » Wed Aug 08, 2012 9:08 am

0xB16B00B5 wrote:Write a program that given 2 ASCII stings as an input, checks if they are exactly equal.
Nope, absolutely wrong. Read this. Please try to first read previous chapters instead of jumping to last one. I know some people think they already know some stuff, when they actually don't. You probably used other programming languages, but C is more "primitive" than modern languages.
0xB16B00B5 wrote:Write a program that checks if one string is part of another (if it's a substring).
  • Use i for iterators, not x
  • You cannot use strlen() as said
  • Avoid using break
  • What happens if a's length is smaller than b's?
  • You don't tell if it's a substring or not...
0xB16B00B5 wrote:I think I'm missing something here.
That means you're using strlen() and you didn't specify where it is located. You're missing the import string.h. Anyway, you cannot use string.h functions for these exercises.
0xB16B00B5 wrote:Write a program that given 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive (that is, given "hello" and "HeLLo", it should say they are equal)
  • You're using literal 5 as the string size. You cannot use literals.
  • You're parsing both strings twice: once to convert them to lower case and then to compare them. This is useless. Try to do everything in one pass.
  • Also when you end the for loop, l = 5, so your while loop will start comparing from 6th character onwards. This is obviously wrong.
  • You say: "The strings are equal, but are case-different.". How you know they were case-different? You shouldn't state this because you simply don't know (and anyway, the exercise didn't ask for such details).
  • Indent better your code and always use braces even if you only have one statement inside of them.
0xB16B00B5 wrote:Yeah, I'm a hard working student.
This is the hacker's way. Nice job investigating by yourself ;)
0xB16B00B5 wrote:Write a program that given two matrices float a[3][3] and float b[3][3], adds them.
Yes, the first one is what a meant.
  • Use i, j, k for counters, not x, y.
  • You're using literals for sizes. Not allowed.
  • Use i < 3 and not x <= 2. This is because the first one shows you the size you're operating with directly, while the second don't.
Try to format the sum matrix output to look like a 3x3 matrix:

Code: Select all

a b c
d e f
g h i
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

0xB16B00B5
Posts: 10
Joined: Tue Jul 24, 2012 6:11 am

Re: [Tutorial] Introduction to programming using C (VIII)

Post by 0xB16B00B5 » Fri Aug 10, 2012 2:33 pm

m0skit0 wrote: Nope, absolutely wrong. Read this. Please try to first read previous chapters instead of jumping to last one. I know some people think they already know some stuff, when they actually don't. You probably used other programming languages, but C is more "primitive" than modern languages.
Second attempt:
[spoiler]

Code: Select all

#include <stdio.h>

#define STRING1 "This is a string"
#define STRING2 "This is a string"

int main()
{
   char s1[] = STRING1;
   char s2[] = STRING2;
   int i;

	for (i = 0; s1[i] == s2[i]; i++) {}

	if (s1[i-1] == '\0' && s2[i-1] == '\0')
	{
		printf("Equal\n");
	}
	else
	{
		printf("Unequal\n");
	}

   return 0;
}
[/spoiler]
Avoid using break
Why?
  • You're using literal 5 as the string size. You cannot use literals.
  • You're parsing both strings twice: once to convert them to lower case and then to compare them. This is useless. Try to do everything in one pass.
  • Also when you end the for loop, l = 5, so your while loop will start comparing from 6th character onwards. This is obviously wrong.
  • You say: "The strings are equal, but are case-different.". How you know they were case-different? You shouldn't state this because you simply don't know (and anyway, the exercise didn't ask for such details).
  • Indent better your code and always use braces even if you only have one statement inside of them.
[spoiler]

Code: Select all

#include <stdio.h>

#define STRING1 "STRING"
#define STRING2 "string"
#define ZERO 0
#define THREETWO 32
#define SIXFOUR 64
#define NINEONE 91

int main()
{
   char s1[] = STRING1;
   char s2[] = STRING2;
   int i = ZERO;
   int equal;   
   
	while (s1[i] != s2[i])
	{	
		if (s1[i] > SIXFOUR && s1[i] < NINEONE)
		{
			s1[i] = s1[i] + THREETWO;
		}
		if (s1[i] == s2[i])
		{
			equal++;
		}
	i++;
	}
   return 0;
}
[/spoiler]
This is the hacker's way. Nice job investigating by yourself ;)
PSV CFW coming soon :lol:
  • Use i, j, k for counters, not x, y.
  • You're using literals for sizes. Not allowed.
  • Use i < 3 and not x <= 2. This is because the first one shows you the size you're operating with directly, while the second don't.
Try to format the sum matrix output to look like a 3x3 matrix:

Code: Select all

a b c
d e f
g h i
[spoiler]

Code: Select all

#include <stdio.h>

#define SIZE 3
#define ZERO 0
#define TWO 2

int main()
{

   float a[SIZE][SIZE] = {4, 2, 7, 1, 7, 2, 5, 6, 2};
   float b[SIZE][SIZE] = {0, 9, 5, 3, 6, 8, 1, 4, 3};
   int i, k;

   for (i = ZERO; i <= TWO; i++)
   {
      for (k = ZERO; k <= TWO; k++)
      {
         printf("%1.0f ", a[i][k]+b[i][k]);
      }
	printf("\n");
   }
   return 0;
}
[/spoiler]

Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Introduction to programming using C (VIII)

Post by Wdingdong » Sun Aug 12, 2012 4:07 pm

m0skit0 wrote:
Wdingdong wrote:Is this okay?
No, it's not. It's worse than before because this has several bugs:
  • Potentially infinite loop
  • Could say false when 2 strings are equal if at least one byte after the NUL character is equal
Your problem here is that you continue comparing past the end of string.
m0skit0 wrote:Why you keep comparing characters when you already know they are not equal? Waste of processing time.
I hope this solves it:
[spoiler]

Code: Select all

#include<stdio.h>
#define SIZE 10

int main()
{
   char s1[SIZE] = "equalll";
   char s2[SIZE] = "equal";
   int i = 0;               //Used as counter and loop variable
   while(s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
   {
      i++;
   }
   if(s1[i] == s2[i])
   {
      printf("The strings are equal");
   }
   else
   {
      printf("The strings are not equal");
   }
   return 0;
}
[/spoiler]
// Big thanks to people who share information !!!

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VIII)

Post by m0skit0 » Mon Aug 13, 2012 12:04 pm

0xB16B00B5 wrote:Second attempt
Way better, but still wrong. Now you have 2 bugs:
  • Segment violation condition
  • Saying 2 strings are different when they are equal
I'll try to explain you why your solution is wrong with 2 examples (one for each bug):

Let's do this for initialization:

Code: Select all

char *s1 = STRING1;
char *s2 = s1;
If you try this initialization, you'll get a segment violation, despite that there's nothing weird except that both strings are actually the same one (not only the have the same contents, but s1 and s2 are the same memory address, same pointer value).

Code: Select all

$ gcc Prueba.c
$ ./a.out 
fish: Job 1, “./a.out ” terminated by signal SIGSEGV (Address boundary error)
And now let's suppose this initialization:

Code: Select all

#define STRING1 "foo\0ohnoes!"
#define STRING2 "foo\0ohyeah!"
[...]
char *s1 = STRING1;
char *s2 = STRING2;
Nota that STRING1 and STRING2 are equal strings, because a string in C ends at NUL character (\0). When you run your code:

Code: Select all

$ gcc Prueba.c
$ ./a.out
Unequal
Both bugs have the same root cause... Can you spot it?
0xB16B00B5 wrote:Why?
It is said it makes code less readable. In your case though, it does not, but it's a good exercise for you to avoid it, and find an alternative solution. Once you understand why break can break legibility, then you can use it ;)

Code: Select all

#define ZERO 0
#define THREETWO 32
#define SIXFOUR 64
#define NINEONE 91
Cool but really awful naming. First, literals like 0 do not need macros because such values are very unlikely to change. THREETWO is a horrible name because it says nothing about the meaning of that value. What about CASE_GAP? Same goes for SIXFOUR, which in this case is even worse because 64 is not an ASCII code you care about. You care about 65 and 90, not 64 and 91. So

Code: Select all

#define UPPERCASE_A 65
#define UPPERCASE_Z 90
is a much wiser choice.

You are not telling if the strings are equal or not, and it also has the same problems than your previous exercise.
0xB16B00B5 wrote:PSV CFW coming soon :lol:
Why not? ;) But I think you misunderstand what hacker actually means.
m0skit0 wrote:Use i < 3 and not x <= 2
m0skit0 wrote:Try to format the sum matrix output to look like a 3x3 matrix
I don't see this.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VIII)

Post by m0skit0 » Mon Aug 13, 2012 12:10 pm

Wdingdong wrote:I hope this solves it
Wasn't that hard, was it? ;) Also, do you actually need SIZE for anything?
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Sirius
Posts: 103
Joined: Sat Dec 18, 2010 3:31 pm

Re: [Tutorial] Introduction to programming using C (VIII)

Post by Sirius » Fri Aug 24, 2012 6:17 am

Hi moskito, first sorry for the bad english
I did everything in the same file, but everything is divided properly. Sometimes I didnt use "{" or "}" just to make the code smaller, usually I dont do that :P

[spoiler]

Code: Select all

#include <stdio.h>

#define MAX 10

int main(int argc, char* argv[])
{
	int vec[] = {5,23,54,76,12,56,4,8,78,1};
	int i;
	int cont=0;
	//-------------------------------------------------
	//Even and odd numbers...
	printf("Even numbers\n");
	for(i=0;i<MAX;i++)
		if(vec[i] % 2 == 0)
			printf("%d ", vec[i]);
	
	//-------------------------------------------------
	//Odd numbers ... 
	printf("\nOdd numbers\n");
	for(i=0;i<MAX;i++)
		if(vec[i] % 2 != 0)
			printf("%d ",vec[i]);
			
	//-------------------------------------------------
	//prints only the 2 first multiple of 3 numbers.
	printf("\nprints only the 2 first multiple of 3 numbers.\n");
	for(i=0;i<MAX,cont<2;i++)
	{
		if(vec[i] % 3 == 0)
		{
			cont++;
			printf("%d ", vec[i]);
		}
	}	
	
	//-------------------------------------------------
	//given one string, prints its length.
	printf("\ngiven one string, prints its length.\n");
	char str[] = "Nice tutorial!";
	for(i=0;str[i] != '\0';i++)
		;
	printf("The lenght of \"%s!\" is %d\n",str,i);
	
	//-------------------------------------------------
	//given 2 ASCII stings as an input, checks if they are exactly equal
	printf("given 2 ASCII stings as an input, checks if they are exactly equal\n");
	char str1[] = "Moskito awesome";
	char str2[] = "Moskito awesome!";
	printf("%s\n%s\n",str1,str2);
	for(i=0;str1[i] != '\0';i++)
		if(str1[i] != str2[i])
			break;
	if(str1[i] == '\0')
		printf("They are equal\n");
	else
		printf("They are not equal\n");
		
	//-------------------------------------------------
	//checks if one string is part of another (if it's a substring).
	printf("checks if one string is part of another (if it's a substring).\n");
	char str3[] = "This is too easy";
	char str4[] = "too easy";
	int j=0;
	printf("%s\n%s\n",str3,str4);
	for(i=0;str3[i] != '\0';i++)
	{
		if(str4[0]==str3[i])
			while(str4[j] != '\0' && str4[j++] == str3[i++])
				;
	}
	if(str4[j] == '\0')
		printf("%s is a substring of %s\n",str4, str3);
	else
		printf("%s is not a substring of %s\n",str4, str3);
		
	
	//-------------------------------------------------
	//given an array of 10 numbers, it orders it ascendantly.
	printf("given an array of 10 numbers, it orders it ascendantly.\n");
	for(i=0;i<MAX;i++)  //just to print it...
		printf("%d ",vec[i]);
	int posMenor=0;
	int aux;
	for(i=0;i<MAX;i++)
	{
		for(j=i+1;j<MAX;j++)
		{
			if(vec[j]<vec[posMenor])
				posMenor = j;
		}
		aux = vec[i];
		vec[i] = vec[posMenor];
		vec[posMenor] = aux;
		posMenor = i+1;
	}
	printf("\nOrded ascendantly\n");
	for(i=0;i<MAX;i++)  //just to print it...
		printf("%d ",vec[i]);	
		
	
	//-------------------------------------------------
	//given 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive
	printf("\ngiven 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive\n");
	char str5[50],str6[50];
	printf("Write 2 string: ");
	scanf("%s", str5);
	scanf("%s", str6);
	for(i=0;str5[i] != '\0';i++)
		if(str5[i] != str6[i])
		{
			if(str5[i]<50 && str6[i] > 50 && str5[i]+20 != str6[i]) //upper + lower
				break;
			if(str5[i]>50 && str6[i] < 50 && str5[i] != str6[i]+20)//lower + upper
				break;
		}
				
				 
	if(str5[i] == '\0')
		printf("They are equal\n");
	else
		printf("They are not equal\n");
	
	
	//-------------------------------------------------
	//given two matrices float a[3][3] and float b[3][3], adds them.
	printf("\ngiven two matrices float a[3][3] and float b[3][3], adds them.\n");
	float a[3][3] = {{2.3,3,4},{4,2,7},{5,8,9}};
	float b[3][3] = {{3.2,5,8},{1,5,7},{3,2,5}};
	for(i=0;i<3;i++)
		for(j=0;j<3;j++)
			a[i][j] += b[i][j];
	//just to print...
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
			printf("%.1f ",a[i][j]);
		printf("\n");
	}
	
	
	return 0;
}
[/spoiler]

Nice tutos ;D

SifJar
Posts: 251
Joined: Tue Jan 11, 2011 10:19 pm

Re: [Tutorial] Introduction to programming using C (VIII)

Post by SifJar » Sat Mar 09, 2013 11:18 pm

Quite a few exercises here...

Write a program that given an array of 10 numbers, prints only the even numbers.
[spoiler]

Code: Select all

#include <stdio.h>

#define ARRAY_SIZE 10

int main()
{
	int array[ARRAY_SIZE] = {2, 5, 9, 10, 45, 5, 1, 3, 4, 12};
	int i;

	for(i = 0; i < ARRAY_SIZE; i++){
		if(array[i]%2 == 0){
			printf("%d, ", array[i]);
		}
	}

	return 0;
}
[/spoiler]

Write a program that given an array of 10 numbers, prints only the odd positions.
[spoiler]

Code: Select all

#include <stdio.h>

#define ARRAY_SIZE 10

int main()
{
	int array[ARRAY_SIZE] = {2, 5, 9, 10, 45, 5, 1, 3, 4, 12};
	int i;

	for(i = 0; i < ARRAY_SIZE; i+=2){
		printf("%d, ", array[i]);
	}

	return 0;
}
[/spoiler]

Write a program that given an array of 10 numbers, prints only the 2 first multiple of 3 numbers.
[spoiler]

Code: Select all

#include <stdio.h>

#define ARRAY_SIZE 10

int main()
{
	int array[ARRAY_SIZE] = {2, 5, 9, 10, 45, 5, 1, 3, 4, 12};
	int i, j = 0;

	for(i = 0; i < ARRAY_SIZE; i++){
		if(array[i]%3 == 0){
			printf("%d, ", array[i]);
			j++;
		}
		if(j == 2){
			break;
		}

	}

	return 0;
}
[/spoiler]

Write a program that given one string, prints its length.
[spoiler]

Code: Select all

#include <stdio.h>

int main()
{
	char* string = "this is a string";
	int i=0;

	while(*string){
		i++;
		string++;
	}

	printf("length = %d", i);
	return 0;
}
[/spoiler]

Write a program that given 2 ASCII stings as an input, checks if they are exactly equal.
[spoiler]

Code: Select all

#include <stdio.h>

int main()
{
	char* string0 = "this is a string";
	char* string1 = "this is b string";
	int i=0;

	while(*string0){
		if(*string0 != *string1){
			i = 1;
			break;
		}
		string0++;string1++;
	}

	if(i || *string1){ //if two characters aren't equal OR string1 is longer
		printf("Not equal");
	}else{
		printf("Equal");
	}
	return 0;
}
[/spoiler]

Write a program that checks if one string is part of another (if it's a substring).
[spoiler]

Code: Select all

#include <stdio.h>

int main()
{
	char* string0 = "this is a string";
	char* string1 = "is";
	int i=0;

	while(*string0){
		//search until first character matches
		if(*string0 != *string1){
			string0++;
			continue;
		}
		if(*string0 == *string1){
			int old = (int) string0; //store current value of string0
			while(*string1){
				if(*string0 == *string1){ //check subsequent characters
					i = 1; //if characters are matching, i will be 1
				}else{
					i = 0; //if not all characters match, i will be 0
					break;
				}
				string0++;string1++;
			}
			string0 = (char*) old; //restore string0 to keep searching
		}
		if(i){
			break; //stop searching if we found a match
		}
	}

	if(i){
		printf("Is a sub string");
	}else{
		printf("Isn't a sub string");
	}
	return 0;
}
[/spoiler]

Will post more of them some other time. In the mean time, if anyone happens to read these and has any comments, I'd like to hear them.

EDIT: Some more

Write a program that given an array of 10 numbers, it orders it ascendantly.
[spoiler]

Code: Select all

#include <stdio.h>

#define ARRAY_SIZE 10

int main()
{
	int array[ARRAY_SIZE] = {2, 5, 9, 10, 45, 5, 1, 3, 4, 12};
	int i = 0, j = 0, temp = 0;

	for(i = 0; i < ARRAY_SIZE; i++){
		for(j = 0; j < (ARRAY_SIZE - 1); j++){
			if(array[j] > array[j+1]){
				temp = array[j];
				array[j] = array[j+1];
				array[j+1] = temp;
			}
		}
	}

	for(i = 0; i < ARRAY_SIZE; i++){
		printf("%d, ", array[i]);
	}

	return 0;
}
[/spoiler](I got a little inspiration from someone else's solution for this one...)

Write a program that given 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive (that is, given "hello" and "HeLLo", it should say they are equal) This one could be a bit tricky
[spoiler]

Code: Select all

#include <stdio.h>

int main()
{
	char* string0 = "tHIs is a string";
	char* string1 = "this is A stRIng";
	char c1, c2;
	int i=0;

	while(*string0){
		if(*string0 <= 122 && *string0 >= 97){
			c1 = *string0 - 32;
		}else{
			c1 = *string0;
		}
		if(*string1 <= 122 && *string1 >= 97){
			c2 = *string1 - 32;
		}else{
			c2 = *string1;
		}
		if(c1 != c2){
			i = 1;
			break;
		}
		string0++;string1++;
	}

	if(i || *string1){ //if two characters aren't equal OR string1 is longer
		printf("Not equal");
	}else{
		printf("Equal");
	}
	return 0;
}
[/spoiler](Actually didn't find this one too tricky...)

Write a program that given two matrices float a[3][3] and float b[3][3], adds them.
[spoiler]

Code: Select all

#include <stdio.h>

#define DIMENSION 3

int main()
{
	float matrix1[DIMENSION][DIMENSION] = {{1.5,2.5,3.5},{4.5,5.5,6.5},{7.5,8.5,9.5}};
	float matrix2[DIMENSION][DIMENSION] = {{1.1,1.2,1.3},{2.1,2.2,2.3},{3.1,3.2,3.3}};
	float result[DIMENSION][DIMENSION] = {};
	int i = 0, j = 0;

	for(i = 0; i < DIMENSION; i++){
		for(j = 0; j < DIMENSION; j++){
			result[i][j] = matrix1[i][j] + matrix2[i][j];
		}
		printf("%f, %f, %f\n", result[i][0], result[i][1], result[i][2]);
	}

	return 0;
}
[/spoiler]

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VIII)

Post by m0skit0 » Thu Mar 14, 2013 1:19 pm

Cool, I've checked some. Nice work ;) Since this is bread and butter for you, let's go for some more ;)
  • Write a program that given an array of 10 numbers, prints only the 2 first multiple of 3 numbers.
    Use while() instead of for() and get rid of break.
  • Write a program that given one string, prints its length.
    Print the string along with the length (e.g. "The string %s has length %d\n") without using an auxiliary pointer.
  • Write a program that given 2 ASCII stings as an input, checks if they are exactly equal.
    Same as previous :mrgreen:
  • Write a program that checks if one string is part of another (if it's a substring).
    Write the same program without using continue.
    Write the same program without using any loop construct (for, while, etc...) :mrgreen:
  • Write a program that given 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive (that is, given "hello" and "HeLLo", it should say they are equal)
    Rewrite using while() instead of for() and get rid of break.
  • Write a program that given two matrices float a[3][3] and float b[3][3], adds them.
    Multiply them.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Introduction to programming using C (VIII)

Post by Acid_Snake » Fri Mar 15, 2013 11:12 pm

alright I'll give it a try:
Write a program that given 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive (that is, given "hello" and "HeLLo", it should say they are equal)
Rewrite using while() instead of for() and get rid of break.
[spoiler]

Code: Select all

str1 = raw_input("> ")
str2 = raw_input("> ")
if str1.upper() == str2.upper(): print "They are equal"
else: print "They are not equal, suck my ***"
[/spoiler]
now I'm bored so I won't make any more exercises.

Post Reply

Return to “Programming and Security”