Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C] Competition Problems
06-22-2010, 06:28 AM
Post: #11
RE: [C] Competition Problems
I understood the malloc and size of part Tongue

I just needed help understanding the logic, thanks for the explanation though!

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2010, 07:03 PM
Post: #12
RE: [C] Competition Problems
(06-21-2010 12:42 AM)drdebcol Wrote:  Well basically, that first one is Euclidean distance between points in 2d.
You have it illustrated here :
http://www.pro9ramming.com/length-of-pat...-t-88.html
And i would like to change the code a bit.
First you don't have to call power function, you can just make a macro statement and define sqr or sqare :
Code:
#define sqr(x)  (x)*(x)
You don't need a function for distance also, put it in a printf(). I understand if you wanted to make it to look better Smile
Also you printed it with conversion "%d" and that is float number.
If you printf a float number (that is FP-Floating Point) with integer conversion, you can get something really different than the thing you wanted.
So changed code should look like this :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define sqr(x)  (x)*(x)

int main()
{
float x1,y1,x2,y2;
printf("Enter in X1 Y1(Seperated by Spaces)  : ");
scanf("%f %f",&x1,&y1);
printf("Enter in X2 Y2 (Seperated by Spaces) : ");
scanf("%f %f",&x2,&y2);
printf("Distance: %f\n",sqrt(sqr(x2-x1)+sqr(y2-y1)));
system("pause");
}
P.S. I included <stdlib.h> because i needed system("pause") at the end.

Well second task, is almost the same as this here :
http://www.pro9ramming.com/decomposition...t-604.html
The task i did in that thread is a decomposition of prime factors.
And this task that Back_track did is a decomposition to all factors.
It is very good solution Back_track.
I changed it to look more like C lol :
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
printf("Enter Number: ");
scanf("%d",&num);
int i;
  for(i=1;i<=num;i++)
    if(num % i == 0)
      printf("%d\n",i);
  system("pause");
}

And about third task. Well because you printed float value on one decimal, you could use "float Farenheit" or "double Farenheit" except "int Farenheit".

That would be all. Generally good job Back_track !!!

Also this one that Codecaine suggested :
Have a user enter 20 positive integers in a array. Then only print out the integers that are not a duplicate.
Well i did it for n integers. And i used "unsigned" because you said "positive integers".
Also i used dynamic memory allocation with function malloc(), so that user can enter length of array that he wants, without
limits (of course memory is always the limit).
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned i,j,n,k,*a;
scanf("%d",&n);
  a=(unsigned*)malloc(n*sizeof(unsigned));
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  for(i=0;i<n;i++)
    {
      k=1;
       for (j=0;j<n;j++)
         if (j!=i && a[i]==a[j])
          {
             k=0;
             break;      
          }
           if (k)
             printf("%d\n",a[i]);              
    }
system("pause");
}

Nice work I did it in c++ but currently not on my home computer. When I have a chance I will post the source.
Visit this user's website Find all posts by this user
Quote this message in a reply
07-24-2010, 07:06 PM
Post: #13
RE: [C] Competition Problems
Well that is a bit advanced part of C, because you need to know pointers, but generally very useful !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: