Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C] Competition Problems
06-20-2010, 01:11 PM
Post: #1
[C] Competition Problems
Distance Between Points
Code:
/*
*Compile with g++ -o Distance Distance.c
*Competition Problem Done By Back_Track
*
*/

#include <stdio.h>
#include <math.h>

int Distance(int x1,int y1,int x2,int y2);

int main(int argc, char *argv[]){
int x1,y1,x2,y2;
printf("Enter in X1 Y1(Seperated by Spaces)  : ");
scanf("%d %d",&x1,&y1);
printf("Enter in X2 Y2 (Seperated by Spaces) : ");
scanf("%d %d",&x2,&y2);
printf("Distance: %d\n",Distance(x1,y1,x2,y2));

return 0;
}

int Distance(int x1,int y1,int x2, int y2){
    int d = pow((x2 - x1),2)  + pow((y2-y1),2);

    return sqrt(d);
}

Factors

Code:
/*
*Compile with gcc -o factors factors.c
*Competition Problem Done By Back_Track
*
*/

#include <stdio.h>

int main(int argc , char *argv[]){
int num;
printf("Enter Number: ");
scanf("%d",&num);
int i = 1;
for(i; i <= num; i++){
    
    if(num % i == 0){
    printf("%d\n",i);
    }
}


return 0;
}

Fahrenheit-Celsius
Code:
/*
*Compile with gcc -o Far Far_Cel.c
*Competition Problem Done By Back_Track
*
*/

#include <stdio.h>

int main(int argc, char *argv[]){
    int Far;
    printf("Enter Farenheit Value: ");
    scanf("%d",&Far);
    float value = (5.0/9.0) * (Far-32.0);
    printf("Celcius: %0.1f\n",value);

return 0;
}

"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
06-20-2010, 02:42 PM
Post: #2
RE: [C] Competition Problems
Here is a challenge. Should be simple but good exercise. Have a user enter 20 positive integers in a array. Then only print out the integers that are not a duplicate.
Visit this user's website Find all posts by this user
Quote this message in a reply
06-20-2010, 02:46 PM
Post: #3
RE: [C] Competition Problems
Heh, ill try/

"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
06-20-2010, 02:57 PM
Post: #4
RE: [C] Competition Problems
(06-20-2010 02:46 PM)Back_track Wrote:  Heh, ill try/

It was in a howto program in java book. It seemed really easy but it took me a while!
Visit this user's website Find all posts by this user
Quote this message in a reply
06-20-2010, 03:06 PM
Post: #5
RE: [C] Competition Problems
Damn...this is kind of a tough one. I've tried reversing the array and comparing it to itself/ But that would only work if it was the same in the opposite position. I think this could be easier achieved in python where you could add all the number to a list and check the array against that

"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
06-20-2010, 06:49 PM
Post: #6
RE: [C] Competition Problems
Nice task for exercise Smile I am not really pro at C languages, but I can do it! Tongue Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
06-21-2010, 12:19 AM
Post: #7
RE: [C] Competition Problems
(06-20-2010 06:49 PM)l3g1sl4tor Wrote:  Nice task for exercise Smile I am not really pro at C languages, but I can do it! Tongue Smile

Java, c++, or pascal is accepted too. Lanuages like ruby and python have easy built in functions to do it.
Visit this user's website Find all posts by this user
Quote this message in a reply
06-21-2010, 12:42 AM (This post was last modified: 07-30-2010 08:26 PM by drdebcol.)
Post: #8
RE: [C] Competition Problems
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]);              
    }
    free(a);
system("pause");
}

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
06-22-2010, 05:43 AM
Post: #9
RE: [C] Competition Problems
Hmmm Drdebcol. I get the gist of the problem you solved but can you explain it a bit better?

"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
06-22-2010, 06:23 AM
Post: #10
RE: [C] Competition Problems
(06-22-2010 05:43 AM)Back_track Wrote:  Hmmm Drdebcol. I get the gist of the problem you solved but can you explain it a bit better?
Well this is an easy task. You have 2 loops (i and j) going through all members of array. If members are different (that means that i is not equal to j or i!=j) and a[i]=a[j] that means that member is a duplicate.
You just need to est k to zero (false) and later in (i) loop check if k=1 (true) and than print that number.
And about malloc() function find on the internet.

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: