Like Tree4Likes
  • 2 Post By oogabooga
  • 1 Post By laserlight
  • 1 Post By Salem

Vector os structs not working how it is supposed to...

This is a discussion on Vector os structs not working how it is supposed to... within the C Programming forums, part of the General Programming Boards category; got to write this little program, that reads some points coordinates and do some stuff with it, can't figure out ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    6

    Vector os structs not working how it is supposed to...

    got to write this little program, that reads some points coordinates and do some stuff with it, can't figure out why it doesn't work for more than two points. here is the code:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    #define DISTANCE sqrt((u[a].x-u[b].x)*(u[a].x-u[b].x)+(u[a].y-u[b].y)*(u[a].y-u[b].y)+(u[a].z-u[b].z)*(u[a].z-u[b].z))
    
    
    typedef struct{
    	int x, y, z;
    } point;
    
    
    typedef point* dots;
    
    
    //building the structures
    
    
    dots build(int* num) {
    	int size;
    	dots Temp;
    	
    	do{
    		printf("Type number of points: ");
    		scanf("%d", &size);
    		if(size<=0) puts("Invalid Number.");
    	}while (size<=0);
    	
    	Temp = (dots) malloc (sizeof(dots)*size);
    	if (Temp == NULL) exit(1);
    	*num = size;
    	return Temp;
    }
    
    
    //reading the points
    
    
    void readpoints(dots u, int size){ //u stands for Universe
    	int i;
    	
    	for(i=0; i<size; i++){
    		system("cls");
    		printf("Point %d: ",  (i+1));
    		printf("\nType x coordinate: ");
    		scanf("%d", &u[i].x);
    		printf("\nType y coordinate: ");
    		scanf("%d", &u[i].y);
    		printf("\nType z coordinate: ");
    		scanf("%d", &u[i].z);
    	}
    }
    
    
    //printing the points out
    
    
    void printp(dots u, int size){
    	int i;
    	
    	printf("Typed points:\n");
    	for(i=0; i<size; i++)
    		printf("Point %d: {%d, %d, %d}\n", (i+1), u[i].x, u[i].y, u[i].z);
    	system("PAUSE");
    
    
    }
    
    
    //one funcion for both requests, based on choice
    
    
    void distance(int choice, dots u, int size){
    	float dis;
    	if (!choice) {
    		int a, b;
    		dis=0;
    		printf("Type the two points (1 - %d): ", size);
    		scanf("%d %d", &a, &b);
    		a--; b--;
    		dis = DISTANCE;
    		system("cls");
    		printf("Distance from %d to %d = %f\n", (a+1), (b+1), dis);
    	}
    	else {
    		int a, b;
    		for (a=0; a<size; a++){
    			dis=0;
    			b=a+1;
    			dis = DISTANCE;
    			system("cls");
    			printf("Distance from %d to %d = %f\n", (a+1), (b+1), dis);
    		}
    	}
    	system("PAUSE");
    }
    
    
    //menu
    
    
    void menu() {
    	int choice, num;
    	dots universe;
    	printf("First we need some POINTS!\n");
    	universe = build(&num);
    	readpoints(universe, num);
    	do{
    		system("cls");
    		printf("Type\n");
    		printf("1: Retype points.\n");
    		printf("2: Print typed points.\n");
    		printf("3: Calculate distance between two points.\n");
    		printf("4: Calculate distance one by one(1-2, 2-3...)\n");
    		printf("5: Exit\nChoice: ");
    		scanf("%d", &choice);
    		switch (choice){
    			case 1: universe = build(&num); readpoints(universe, num); break;
    			case 2: printp(universe, num); break;
    			case 3:
    			case 4: distance((choice-3), universe, num); break;
    			case 5: choice = 0; break;
    			default: 	printf("Invalid entry");
    						system("PAUSE");
    						choice = 1;
    						break;
    		}
    	}while(choice);
    }
    
    
    //simple main
    
    
    int main(){
    	menu();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,193
    Code:
    Temp = (dots) malloc (sizeof(dots)*size);
    You're allocating memory based on the size of a pointer to a structure, not the size of the structure itself.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    First and foremost you have to get rid of the "dots" typedef. It makes your program harder to read by hiding the fact that it's a pointer. Just use point* everywhere you use dots.

    Your malloc call in build is wrong. It's allocating space for size POINTERS to point, but you want size point's, like this:
    Code:
    	Temp = malloc (sizeof(point) * size);
    Your macro is a little unusual. It'd be more normal, and potentially more useful, to parameterize it like this:
    Code:
    #define DISTANCE(u,a,b) sqrt((u[a].x-u[b].x)*(u[a].x-u[b].x)+  \
                                 (u[a].y-u[b].y)*(u[a].y-u[b].y)+  \
                                 (u[a].z-u[b].z)*(u[a].z-u[b].z))
    And you would call it like this: DISTANCE(u,a,b);
    memcpy and antred like this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    6
    oh, that makes much more sense than what i was doing...

    i will also redefine the macro, thanks!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Incidentally, it is usually good practice to parenthesize identifiers used in a function-style macro in case the identifier is replaced with an expression and precedence changes the meaning of the code. The brackets in the array index notation serve the same purpose, but for u:
    Code:
    #define DISTANCE(u, a, b) sqrt(((u)[a].x - (u)[b].x) * ((u)[a].x - (u)[b].x) + \
                                   ((u)[a].y - (u)[b].y) * ((u)[a].y - (u)[b].y) + \
                                   ((u)[a].z - (u)[b].z) * ((u)[a].z - (u)[b].z))
    oogabooga likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > i will also redefine the macro, thanks!
    It would be better to redefine it as a proper function.

    The only reason for writing macros is performance optimisation, and you're nowhere near that kind of issue yet. Most modern C compilers support inline functions to begin with.


    The first rule of optimisation-club is "Don't do it!"
    The second rule of optimisation-club is "Don't do it! (yet)"
    antred likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A vector of structs problem
    By Swordsalot in forum C++ Programming
    Replies: 4
    Last Post: 01-05-2011, 02:46 AM
  2. Sorting a vector of structs....
    By ropitch in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2009, 11:49 PM
  3. Sorting Vector of structs.
    By Zosden in forum C++ Programming
    Replies: 41
    Last Post: 10-22-2008, 12:28 AM
  4. Sorting Vector of Structs in STL
    By creativeinspira in forum C++ Programming
    Replies: 5
    Last Post: 08-07-2007, 01:54 AM
  5. Vector of Structs Question
    By gamer4life687 in forum C++ Programming
    Replies: 6
    Last Post: 09-09-2005, 10:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21