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.
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 Jul 19, 2012 4:22 pm

Ok, here go my comments:

[spoiler]

Code: Select all

#define size 10
Constants go all caps. E.g. SIZE. Not written anywhere; it's a convention.

Code: Select all

c += 1;
And why not c++ :lol: Also use a more descriptive name for this variable.[/spoiler]
Wdingdong wrote:I didn't understand this: "2 first multiple of 3 numbers."
I express myself like an ***. I meant the 2 first numbers that are multiple of 3.
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

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

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

Post by Wdingdong » Wed Jul 25, 2012 6:15 pm

m0skit0 wrote:And why not c++
Hehe, So stupid of me :D May be due to rote learning system in my country :?

Okay,
Ascending sorting

Code: Select all

#include<stdio.h>
#define SIZE 10

int main()
{
	int a [SIZE] = { 4, 6, 7, 3 ,2, 1, 0, 9, 5, 8};
	int i, j, temp;
	for(i = 0; i < SIZE; i++)
	{
		for(j = 0; j < (SIZE - 1); j++)
		{
			if(a[j] > a[j+1])
			{
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
	}
	printf("Array after sorting: \n");
	for(i = 0; i < SIZE; i++)
	{
		printf("%d\n", a[i]);
	}
	return 0;
}
Check Equal Strings(Case Sensitive)

Code: Select all

#include<stdio.h>
#define SIZE 10

int main()
{
	char s1[SIZE] = "equal";
	char s2[SIZE] = "Equal";
	int i;
	int count = 0;				//Counter to count unequal characters
	for(i = 0; s1[i] != '\0' || s2[i] != '\0'; i++)
	{
		if(s1[i] != s2[i])
		{
			count++;
		}
	}
	if(count == 0)
		printf("Strings are equal");
	else
		printf("Strings are not equal");
	return 0;
}
Check Equal Strings(Not Case Sensitive)

Code: Select all

#include<stdio.h>
#define SIZE 10

int main()
{
	char s1[SIZE] = "equalL";
	char s2[SIZE] = "Equall";
	int i;
	int count = 0;				//Counter to count unequal characters
	for(i = 0; s1[i] != '\0' || s2[i] != '\0'; i++)
	{
		if(s1[i] != s2[i])
		{
			if(s1[i] > s2[i] && s1[i] - 32 != s2[i])		//The difference between value of upper and lower case is 32
				count++;
			else if(s2[i] > s1[i] && s2[i] -  32 != s1[i])
				count++;
		}
	}
	if(count == 0)
		printf("Strings are equal");
	else
		printf("Strings are not equal");
	return 0;
}
Three First Multiple of 3

Code: Select all

#include<stdio.h>
#define SIZE 10

int main()
{
	int i, j = 0;
	int a[SIZE] = {1, 3, 5, 6, 9, 12, 15, 9, 6, 9};
	for(i = 0; i < SIZE; i++)
	{
		if(a[i] % 3 == 0 && j < 3)
		{
			printf("%d\n",a[i]);
			j++;
		}
	}
	return 0;
}
Matrix Addition

Code: Select all

#include<stdio.h>

int main()
{
	float a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
	float b[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
	float c[3][3];
	int i, j;
	printf("Matrix Addition:\n");
	for(i = 0; i < 3; i++)
	{
		for(j = 0; j < 3; j++)
		{
			c[i][j] = a[i][j] + b[i][j];
			printf("%f\t", c[i][j]);
		}
		printf("\n");
	}
	return 0;
}
I'm stuck with checking the substring. It'd be good if you help a little :)
Advertising
// 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 » Tue Jul 31, 2012 7:19 am

Sorry for the lateness :roll:
Wdingdong wrote:Ascending sorting
Ok ;)
Wdingdong wrote:Check Equal Strings(Case Sensitive)
Why you keep comparing characters when you already know they are not equal? Waste of processing time.
Wdingdong wrote:Check Equal Strings(Not Case Sensitive)
Same as previous one and also:

Code: Select all

         if(s1[i] > s2[i] && s1[i] - 32 != s2[i])      //The difference between value of upper and lower case is 32
            count++;
         else if(s2[i] > s1[i] && s2[i] -  32 != s1[i])
            count++;
Always use {}. You'll save yourself headaches later. Also don't you think this kind of cumbersome? Can you see a better way to do this instead of having those ifs and -32 repeated?
Wdingdong wrote:Three First Multiple of 3
There's no such exercise... You probably meant the 2 first multiples of 3. Anyway, you keep reading the array even if you don't need to (same error as 2 previous exercises).
Wdingdong wrote:Matrix Addition
Ok ;)
Wdingdong wrote:I'm stuck with checking the substring. It'd be good if you help a little :)
What's the problem?
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 » Thu Aug 02, 2012 1:13 pm

m0skit0 wrote: [*]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

main()
{
	int array[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int a;

	for (a = 0; a <= 9; a++)
	{
		if (array[a] % 2 == 0)
		printf("%d\n", array[a]);
	}
}  
[/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

main()
{
	int array[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int a;

	for (a = 0; a <= 9; a++)
	{
		if (array[a] % 2 == 1)
		printf("[%d] ", a+1);
	}
	printf("\n");
}  
[/spoiler]
Doesn't actually print the position in the array. Oh, and if the "a+1" looks weird, I'm just expirementing with stuff.
The output gives me even numbers, so a+1 makes them odd.
I don't know (I have a horrible memory so sorry if you've already covered that) how to actually print the position from the array.
[*]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

main()
{
	int array[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int a, b;

	a = 0;
	b = 0;

	while (b < 2)
	{
		if (array[a] % 3 == 0)
		{
		printf("%d\n", array[a]);
		b++;
		}
	a++;
	}
}
[/spoiler]
Tried getting a bit creative for this one.
[*]Write a program that given one string, prints its length.
[spoiler]

Code: Select all

#include <stdio.h>

#define ARRAY_SIZE 10

main()
{
	int a;

	while (getchar() != EOF)
	a++;

	printf("%d", a);
}
[/spoiler]
Not sure if that's what you wanted.. correct me if I'm wrong.

I'll do the others later.

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 Aug 02, 2012 2:50 pm

Cool. I spoiled your answers so you don't spoil stuff to people trying to do the exercises themselves ;)

And some comments:

Look to format better your code. Check my code on how to use indentation correctly.

Code: Select all

main()
Wrong main() declaration. main() must return int in standard C.

Code: Select all

      if (array[a] % 2 == 0)
      printf("%d\n", array[a]);
Never use ifs without braces. You'll avoid headaches in the future.

Code: Select all

for (a = 0; a <= 9; a++)
You're using a literal here. You actually don't need to. That's why you have ARRAY_SIZE.
0xB16B00B5 wrote:Write a program that given an array of 10 numbers, prints only the odd positions.
You understood bad the goal of this exercise. You have to print the values of the array that are in odd positions, taking position in the array like 1st element (odd) is index 0, 2nd element (even) is index 1, and so on.
0xB16B00B5 wrote:Oh, and if the "a+1" looks weird, I'm just expirementing with stuff.
It does not. I'm a programmer, dude :lol:
0xB16B00B5 wrote:I don't know (I have a horrible memory so sorry if you've already covered that) how to actually print the position from the array.
:shock: You do it in the first exercise ;)
0xB16B00B5 wrote:Tried getting a bit creative for this one.
I don't see that much creativity, but only the best way to do it. Congrats! ;)
0xB16B00B5 wrote:Not sure if that's what you wanted.. correct me if I'm wrong.
Not really, although your code is OK. Take this skeleton and try to complete it to do what's asked:

Code: Select all

#include <stdio.h>

int main()
{
	char* target = "This is the string you have to calculate its length";
	int length = 0;
	
	// Put your code here!
	
	print("The length of the target string is %d\n", length);
	
	return 0;
}

Code: Select all

   while (getchar() != EOF)
   a++;
Same as with ifs: never use while without braces. This is friend's advice! :lol:
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

PrimeNexes
Posts: 8
Joined: Thu Aug 02, 2012 2:45 pm

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

Post by PrimeNexes » Thu Aug 02, 2012 3:04 pm

I know the C and some C++ I wanted to know what more is required to write for PSP ?

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 » Fri Aug 03, 2012 11:43 am

Nothing more if you can read documentation and know how to install the tools.

Also just FYI: I've heard a lot of people saying they already know C/C++ and couldn't understand PSPSDK documentation... This simply means you don't know C/C++. Knowing C/C++ is not knowing how to write a "Hello World" for Windows. It's knowing how C/C++ works. C/C++ works the same no matter the platform. Only system calls change from one platform to another.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

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

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

Post by Wdingdong » Fri Aug 03, 2012 5:12 pm

m0skit0 wrote:Why you keep comparing characters when you already know they are not equal? Waste of processing time.
Is this okay?
CHECK EQUAL STRINGS(Case sensitive)

Code: Select all

#include<stdio.h>
#define SIZE 10

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

PrimeNexes
Posts: 8
Joined: Thu Aug 02, 2012 2:45 pm

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

Post by PrimeNexes » Sat Aug 04, 2012 5:57 pm

Thanks for your reply m0skit0 .I will try my best .

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 06, 2012 9:42 am

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.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Post Reply

Return to “Programming and Security”