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!
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){
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, 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
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.
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...
This post has been edited by ifigeneia: 8 Jun, 2007 - 10:44 PM
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){