Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,133 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,816 people online right now. Registration is fast and FREE... Join Now!




What is wrong with my code?

 
Reply to this topicStart new topic

What is wrong with my code?

ifigeneia
8 Jun, 2007 - 08:52 AM
Post #1

New D.I.C Head
*

Joined: 8 Jun, 2007
Posts: 4


My Contributions
I am a student and i just started learning programming in C....
Anyway, i was given a homework and i started doing it ,but now i am stuck!!!
Part of the Homework is this...
The user gives two arrays which describe maximum 50 locations(which are defined with pairs of (x,y) variables)...The program should register them and then should print the arrays but without the locations that have distances>5 between them... And this is what i have done:

CODE


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



main ()
{
    int i,j,s,t,n;
    
    float x,y;            
    float r;              
    int l;                  
    int m;                  
    int nl=0;                  
    int nm=0;
    float A[50][2];      
    float B[50][2];      
    
    
    float q,z,p;                  


    printf("Give the number of locations of the first array (max=50)\n\n");
    scanf("%d",&l);
    printf("Give the number of locations of the Second array (max=50\n\n");
    scanf("%d",&m);

    printf("----------------------------------------------------------------------------\n\n");
    printf("You are about to give the data for the first array\n\n");
    
    
    for(n=1; n<l+1; n++){
    printf("Give the (x,y) for the  %d location:\n", n);
    for(i=0, j=0; i<l && j<2; i++, j++){
              if(j==0){
                 printf("x:\n");
                 scanf("%f",&x);
                 A[i][j]=x;
                                
              }
              if(j==1){
              
     printf("y:\n\n");
                 scanf("%f",&y);
     A[i][j]=y;
                
                
              }
       }
    
     }
    i=0;
    j=0;

    printf("-----------------------------------------------------------------------------\n\n");
    printf("For the second array\n\n");

    for(n=1; n<m+1; n++){
    printf("Give the (x,y) for the %d location:\n", n);
      for(i=0, j=0; i<m && j<2; i++, j++){  
              if(j==0){
              
     printf("x:\n");
                 scanf("%f",&x);
                 B[i][j]=x;
                                 
              }
              if(j==1){
              
    printf("y:\n");
                 scanf("%f",&y);
                 B[i][j]=y;
                                 
              }
      }
        
     }
    s=0;
    t=0;
    i=0;
    j=0;
    for (i=0,s=0; i<l && s<m; i++,s++)
    {
        
            z=A[i][0]-B[s][0];
            q=A[i][1]-B[s][1];
            p=pow(z,2)+pow(q,2);
            r=sqrt(p);
            if (r<6){
                for (i=0,s=0; i<l && s<m; i++,s++)
                {
                    for (j=0,t=0; j<2 && t<2; j++,t++)
                    {
                     A[i][j]=A[i+1][j];
                     B[s][t]=B[s+1][t];
                    
                    }
                    nl++;
                    nm++;
                }
            
          
        }
        
    }
    i=0;
    j=0;
    printf("----------------------------------------------------------------------------\n\n");
    printf("The new 1rst array is:\n\n");
    for(n=1; n<(nl+1); n++){
    printf("The %d location:\n", n);
         for(i=0, j=0; i<l && j<2; i++, j++){
              if(j==0){
              
                 printf("x:%f\n",A[i][j]);
                
            
              }
              else{
              
                 printf("y:%f\n\n",A[i][j]);
                
                
              }
         }
        
     }


    i=0;
    j=0;
    printf("----------------------------------------------------------------------------\n\n");
    printf("the new 2nd array:\n\n");
    for(n=1; n<(nm+1); n++){
    printf("The location%d:\n", n);
         for(i=0, j=0; i<m && j<2; i++, j++){
              if(j==0){
              
                 printf("x:%f\n",B[i][j]);
                
            
              }
              else{
              
                 printf("y:%f\n\n",B[i][j]);
                
                
              }
         }
        
     }
    
}  
    
    

User is offlineProfile CardPM
+Quote Post

rameshg87
RE: What Is Wrong With My Code?
8 Jun, 2007 - 09:14 AM
Post #2

New D.I.C Head
Group Icon

Joined: 6 Jun, 2007
Posts: 34


Dream Kudos: 50
My Contributions
Can you make yourself clear in this ? Why do you have two arrays, distance of what points must be less than 5.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: What Is Wrong With My Code?
8 Jun, 2007 - 09:59 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Please describe any error you are receiving, and describe any undesired behaviour being displayed by the application.
User is offlineProfile CardPM
+Quote Post

ifigeneia
RE: What Is Wrong With My Code?
8 Jun, 2007 - 10:15 AM
Post #4

New D.I.C Head
*

Joined: 8 Jun, 2007
Posts: 4


My Contributions
QUOTE(rameshg87 @ 8 Jun, 2007 - 10:14 AM) *

Can you make yourself clear in this ? Why do you have two arrays, distance of what points must be less than 5.

Thanks for answering!!!
Yes you are right... The exact problem is:
We want to build a network of towers (for cellular phones) for two companies ...Each company who is interrested in building its own towers gives an array of the (x,y) location of its towers.The maximum of the towers each company can have is 50.
The rule s that the towers of the different companies should distance 5km AT LEAST.
We are asked to print the arrays of the towers of the two companies(A,cool.gif but without the towers who don't agree to the rule...

I had to translate it, sorry for my english!!!!

The error s receive is that i did'nt manage to get the new corrected A, B arrays... rightly. I must have done something wrong writing the code for the rule...

This post has been edited by ifigeneia: 8 Jun, 2007 - 10:46 PM
User is offlineProfile CardPM
+Quote Post

rameshg87
RE: What Is Wrong With My Code?
8 Jun, 2007 - 06:25 PM
Post #5

New D.I.C Head
Group Icon

Joined: 6 Jun, 2007
Posts: 34


Dream Kudos: 50
My Contributions
Hey, there are mistakes right from the start of the code. Even the array elements are not entered correctly. Check it!!! The problem is your loop. Think carefully while righting the loop. The loop which you presented has error that , when you enter a pair of values for each location (imagine x and y values for A[0]), only the x value is entered into the A[0], the y value goes into A[1], leaving A[0][1] empty and A[1][0] empty. Do carefully while righting loops, because in the problem i don't think you can do it with a loop of this kind

CODE

for(i=0,j=0;i<..........;i++,j++)
{
   .
   .
}


It has to be something of the kind

CODE

for (i=0;i<........;i++)
    for(j=0;j<..........;j++)
{
    .
    .
}


Also check that you are not altering the value of a loop variable inside the loop.


User is offlineProfile CardPM
+Quote Post

ifigeneia
RE: What Is Wrong With My Code?
8 Jun, 2007 - 10:12 PM
Post #6

New D.I.C Head
*

Joined: 8 Jun, 2007
Posts: 4


My Contributions
Thank you very much rameshg87! i'll check it! rolleyes.gif
T

User is offlineProfile CardPM
+Quote Post

ifigeneia
RE: What Is Wrong With My Code?
8 Jun, 2007 - 10:29 PM
Post #7

New D.I.C Head
*

Joined: 8 Jun, 2007
Posts: 4


My Contributions
I checked it and it is not different what you say... I think you write the same thing with me but with different way... the problem must be somewhere where i write the code for the distance between te two towers... But i can't figure what... ph34r.gif

This post has been edited by ifigeneia: 8 Jun, 2007 - 10:44 PM
User is offlineProfile CardPM
+Quote Post

rameshg87
RE: What Is Wrong With My Code?
9 Jun, 2007 - 07:54 AM
Post #8

New D.I.C Head
Group Icon

Joined: 6 Jun, 2007
Posts: 34


Dream Kudos: 50
My Contributions
Well I will still ask you to check the input and the loop during the processing, because I don't think it is right. Just check the inputs whether all of them have been entered just after you enter the inputs before processing them. This is the code that I wrote, modifyting some part of yours. I have commented your code where it required change and replaced there with mine. just check whether this was what you meant !!!

CODE

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



main ()
{
    int i,j,s,t,n;
    
    float x,y;            
    float r;              
    int l;                  
    int m;                  
    int nl=0;                  
    int nm=0;
    float A[50][2];      
    float B[50][2];      
    
    
    float q,z,p;                  


    printf("Give the number of locations of the first array (max=50)\n\n");
    scanf("%d",&l);
    printf("Give the number of locations of the Second array (max=50\n\n");
    scanf("%d",&m);

/*    printf("----------------------------------------------------------------------------\n\n");
    printf("You are about to give the data for the first array\n\n");
    
    
    for(n=1; n<l+1; n++){
    printf("Give the (x,y) for the  %d location:\n", n);
    for(i=0, j=0; i<l && j<2; i++, j++){
              if(j==0){
                 printf("x:\n");
                 scanf("%f",&x);
                 A[i][j]=x;
                                
              }
              if(j==1){
              
     printf("y:\n\n");
                 scanf("%f",&y);
     A[i][j]=y;
                
                
              }
       }
    
     }
    i=0;
    j=0;

    printf("-----------------------------------------------------------------------------\n\n");
    printf("For the second array\n\n");

    for(n=1; n<m+1; n++){
    printf("Give the (x,y) for the %d location:\n", n);
      for(i=0, j=0; i<m && j<2; i++, j++){  
              if(j==0){
              
     printf("x:\n");
                 scanf("%f",&x);
                 B[i][j]=x;
                                
              }
              if(j==1){
              
    printf("y:\n");
                 scanf("%f",&y);
                 B[i][j]=y;
                                
              }
      }
        
     }*/
     printf("Enter first array\n");
    for(i=0;i<l;i++)
     {
                    printf("Enter location %d\n",i+1);
                    printf("x:\n");
                    scanf("%f",&A[i][0]);
                    printf("y:\n");
                    scanf("%f",&A[i][1]);
     }
     printf("Enter second array\n");
    for(i=0;i<m;i++)
     {
                    printf("Enter location %d\n",i+1);
                    printf("x:\n");
                    scanf("%f",&B[i][0]);
                    printf("y:\n");
                    scanf("%f",&B[i][1]);
     }

    
    for(i=0;i<l;i++)
     printf("(%f,%f)\n",A[i][0],A[i][1]);
     printf("\n\n");
    for(i=0;i<m;i++)
     printf("(%f,%f)\n",B[i][0],B[i][1]);

/*    
    s=0;
    t=0;
    i=0;
    
    j=0;

    for (i=0,s=0; i<l && s<m; i++,s++)
    {
  
            z=A[i][0]-B[s][0];
            q=A[i][1]-B[s][1];
            p=pow(z,2)+pow(q,2);
            r=sqrt(p);
            if (r<6){
                for (i=0,s=0; i<l && s<m; i++,s++)
                {
                    for (j=0,t=0; j<2 && t<2; j++,t++)
                    {
                     A[i][j]=A[i+1][j];
                     B[s][t]=B[s+1][t];
                    
                    }
                    nl++;
                    nm++;
                }
            
          
        }
        
    }
    */
    for(i=0;i<l;i++)
        for(j=0;j<l;j++)
        {
            z=A[i][0]-B[j][0];
            q=A[i][1]-B[j][1];
            p=pow(z,2)+pow(q,2);
            r=sqrt(p);
            if(r<6)
            {
                   for(s=i;s<l;s++)
                   {
                       A[s][0]=A[s+1][0];
                       A[s][1]=A[s+1][1];
                   }
                   for(s=j;s<m;s++)
                   {
                       B[s][0]=B[s+1][0];
                       B[s][1]=B[s+1][1];
                   }
                   l--;
                   m--;
                   i--;
                   j--;
                   break;
            }
        }
printf("First array\n");
    for(i=0;i<l;i++)
     printf("(%f,%f)\n",A[i][0],A[i][1]);
     printf("\n\n");
printf("Second array\n");    
    for(i=0;i<m;i++)
     printf("(%f,%f)\n",B[i][0],B[i][1]);
scanf("%c%c",&i,&i);
}


This post has been edited by rameshg87: 9 Jun, 2007 - 07:56 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month