Hey guys,

What I'm trying to do is a function that gets a certain array of structures and print some of the information on the screen, like the name and id_number. The thing is that some fields in the structure are repeated and my goal is to show only one time.

For instance, there are a lot of owners each one has on id_number, but they can have different cars, and I only want my program to show the name and the id_numbers one time for each owner even if the same owner has a 2 or more cars.

here's my code to better understanding of my question:

typedef struct {
  
  int id_number;
  char name[50+1];
  char address [50+1];
  char car[20];
  
}OWNER;

typedef struct {
  
  int count;
  OWNER owners[24000];
  
}SEVERAL_OWNERS;



void show_owners(SEVERAL_OWNERS *list) {
  
  SEVERAL_OWNERS *ptr=NULL;
  int i, j;
  
  for (ptr=list, i=0; i<list->count; i++) {
    for (j=i, j<list->count; j++) {
      if(list->owners[i].id_number == ptr->owners[j].id_number)
	printf("%d %s\n", list->owners[i].id_number, list->owners[i].name);
      break;
    }
  }
}

let's suppose all the fields of the structure are loaded, and what I'm trying is to compare an id_number, which is unique for a certain owner, and search for any other occurrences on the array of structures. If there's not another occurrence I shall print the id_number and the name of the owner on the screen, if so I should only print the same information one time. The problem with this code is that I'm always printing the id_numbers and the names.

If someone could help me out with this I would be very grateful.
Btw sorry for the messy explanation of my problem, English it's not my 1st language...:$

Cheers!

Hey guys,

What I'm trying to do is a function that gets a certain array of structures and print some of the information on the screen, like the name and id_number. The thing is that some fields in the structure are repeated and my goal is to show only one time.

For instance, there are a lot of owners each one has on id_number, but they can have different cars, and I only want my program to show the name and the id_numbers one time for each owner even if the same owner has a 2 or more cars.

here's my code to better understanding of my question:

typedef struct {
  
  int id_number;
  char name[50+1];
  char address [50+1];
  char car[20];
  
}OWNER;

typedef struct {
  
  int count;
  OWNER owners[24000];
  
}SEVERAL_OWNERS;



void show_owners(SEVERAL_OWNERS *list){
  
  SEVERAL_OWNERS *ptr=NULL;
  int i, j;
  
  for (ptr=list, i=0; i<list->count; i++) {
    for (j=i, j<list->count; j++) {
      if(list->owners[i].id_number == ptr->owners[j].id_number)
	printf("%d %s\n", list->owners[i].id_number, list->owners[i].name);
      break;
    }
  }
}

let's suppose all the fields of the structure are loaded, and what I'm trying is to compare an id_number, which is unique for a certain owner, and search for any other occurrences on the array of structures. If there's not another occurrence I shall print the id_number and the name of the owner on the screen, if so I should only print the same information one time. The problem with this code is that I'm always printing the id_numbers and the names.

If someone could help me out with this I would be very grateful.
Btw sorry for the messy explanation of my problem, English it's not my 1st language...:$


Cheers!

First i made some changes in your code to make it more readable..

typedef struct 
{
  
  int id_number;
  char name[50+1];
  char address [50+1];
  char car[20];
  
}OWNER;

typedef struct 
{
  
  int count;
  OWNER owners[24000];
  
}SEVERAL_OWNERS;



void show_owners(SEVERAL_OWNERS *list)
{
  
  SEVERAL_OWNERS *ptr=NULL;
  int i, j;
  
  for (ptr=list, i=0; i<(list->count); i++)
  {
    for (j=i, j<(list->count); j++) 
    {
        if(list->owners[i].id_number == ptr->owners[j].id_number)
	    printf("%d %s\n", list->owners[i].id_number, list->owners[i].name);
        break;
    }/* end of j for loop "The inner loop" */
  }/* end of outer for loop */

}

what you are doing is that you are examining if the

list->owners[i].id_number == ptr->owners[j].id_number

and if they are true..

then

printf("%d %s\n", list->owners[i].id_number, list->owners[i].name);

which means print the id number and the name of the owner only...

which is absolutely what you want ...

you are telling that some fields in the function are repeated ...
what are they because the :-
id number should not be repeated be repeated according to my understanding..

Why don't you just store the last printed ID number and only print the ID number/name if the current ID is different to the last. This assumes you stored records for the same person next to each other in the list. You would only need to traverse the list once using one for loop.

Edited 6 Years Ago by Greg8202: n/a

Suppose x contains the id number that you are looking for

ptr= list;
for(i=0;i<list->count;i++)
{
     // If you find a match then break;
     // Else do nothing
}

if(i==list->size)
// this means we went through the entire list and did not find a match

Edited 6 Years Ago by abhimanipal: n/a

A better aproach will be to divide the problem into several functions.

For example you can sort the id_numbers and then with an sort function, then you can printf the list with a print function, and you can eliminate the same records while printing the function.

Implementing such several loops in another another loop is kiliing the performance and the ease of implementation.

This article has been dead for over six months. Start a new discussion instead.