Thread: cant see what certain part of this code does..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    cant see what certain part of this code does..

    There is an election on 3 colleges.
    For places on the board and for the place of the chairman.
    There are 10 candidates ,each candidate represented by a number from 0 to 9.
    Each student has the right (doesnt have to )to vote 3 times,two votes for members of the board,and one vote for chairman.
    Three files with the votes are coming to the counting committee .
    In each file each vote represented by a line:
    name -30 chars
    id -9 chars
    type of vote("1" for chair man," 2" for board) -1 char
    number of candidate -1 char.

    Each file is sorted by id number in ascending order.
    Write a function.
    void report(FILE* college[3], FILE* arr[10], char* canceled);
    College is the votes files,and it creates 10 new files FILE* arr[10] one file for each candidate.
    Each candidate file concentrates the legal votes for this candidate.
    for each vote in each row we have:
    id -9 chars
    number of college(0-2) -1 char
    type of vote("1" for chair man," 2" for board) -1 char.

    Each file is sorted by id number in ascending order.

    Also the function need to create a file name which is stored in the canceled string for the illegal votes.
    each line has:
    name -30 chars
    id -9 chars
    type of cancellation : "1" if he voted twice for the same candidate.
    "2" if he voted for chairman for more then one time,or if he voted for board for more then two times.
    "3" if he voted from more then one college
    file is sorted by id number in ascending order.

    If a student is mentioned in the canceled file ,his votes cannot be shown in the 10 candidate files.

    i understand this solution till this comment
    //Filling struct table for the minimal id
    i got the idea that index represents the file with minimal id
    Code:
    eof[index]=fscanf(college[index],"%30s%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    currect represent the line of index so they are the same/

    why current.id needs to differ idmin??(its always not)
    why i=1 and not 0?? (files start from 0)

    from this part i cant see what the code means??
    what each subpart does in general
    ??
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    
     
    typedef struct vote {
    	char name [30];
    	int id,type,candidate;
    } vote;
    void report(FILE* college[3], FILE* arr[10], char* canceled);
    // Fuction returns the minimal id
    int getMin(int eof[],vote [],int *);
    // Function checks a vote record correct
    int votecheck(vote ,vote [],int i);
    // Function reads the next record from "college" file
    int readFrom(FILE *,vote [],int);
    
    
    
    int main ()
    {
    FILE* f[10];
    FILE* c[3];
    char*  name="c:\\canceled.txt";
    
    c[0]=fopen("c:\\file1.txt","r");//vote files ,each file represents the votes from a college
    c[1]=fopen("c:\\file2.txt","r");
    c[2]=fopen("c:\\file3.txt","r");
    f[0]=fopen("c:\\cfile1.txt","w");//condidate files
    f[1]=fopen("c:\\cfile2.txt","w");
    f[2]=fopen("c:\\cfile3.txt","w");
    f[3]=fopen("c:\\cfile4.txt","w");
    f[4]=fopen("c:\\cfile5.txt","w");
    f[5]=fopen("c:\\cfile6.txt","w");
    f[6]=fopen("c:\\cfile7.txt","w");
    f[7]=fopen("c:\\cfile8.txt","w");
    f[8]=fopen("c:\\cfile9.txt","w");
    f[9]=fopen("c:\\cfile10.txt","w");
    
    report(c, f, name);
    
    fclose(c[0]);
    fclose(c[1]);
    fclose(c[2]);
    fclose(f[0]);
    fclose(f[1]);
    fclose(f[2]);
    fclose(f[3]);
    fclose(f[4]);
    fclose(f[5]);
    fclose(f[6]);
    fclose(f[7]);
    fclose(f[8]);
    fclose(f[9]);
     
    
    	return 0;
    }
    void  report(FILE *college[3], FILE *arr[10], char* canceled){
    	FILE *cancel;
    	vote currtable[3], // array for three current "college" records
    		votable[3],    // array for checking the records with the same "id"
    		current;	   // struct for the current "college" record	
    	int i,j,idmin,index,flag,eof[3];
    	if(!(cancel=fopen(canceled,"w"))) exit(1);
    	
    	for(i=0;i<3;i++)
    		eof[i]= readFrom(college[i],currtable,i);
    // main loop	
    	while(eof[0]!=EOF||eof[1]!=EOF||eof[2]!=EOF){
    		idmin=getMin(eof,currtable,&index);
    		for(i=flag=0;i<3;i++)
    			if(i!=index && eof[i]!=EOF && currtable[i].id==idmin)
    				flag=3;
    		if(flag){
    		fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			for(i=0;i<3;i++)
    				while(eof[i]!=EOF && currtable[i].id==idmin)
    				eof[i]=readFrom(college[i],currtable,i);
    			continue;
    		}
    //Filling struct table for the minimal id
    		votable[0]=currtable[index];
    		i=1;
    		while(eof[index]!=EOF && i<3){
    			eof[index]=fscanf(college[index],"%30s%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    		if(current.id!=idmin){
    			for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",
    idmin,index,votable[j].type);
    		currtable[index]=current;
    		break;
    		}
    		else{
    			flag=votecheck(current,votable,i);
    			if(flag){				fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			while(eof[index]!=EOF && currtable[index].id==idmin)
    			eof[index]=readFrom(college[index],currtable,index);
    			break;
    			}
    			else
    				votable[i++]=current;
    		}//else
    	}//while
    	if(i<3)  continue;
    		if(eof[index]!=EOF)
    		eof[index]=fscanf(college[index],"%30[^\n]%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    		if(eof[index]==EOF ||current.id!=idmin ){
    			for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",
    idmin,index,votable[j].type);		
    			currtable[index]=current;
    		}
    		else{			fprintf(cancel,"%s%d%d\n",votable[0].name,idmin,2);
    	while(eof[index]!=EOF && currtable[index].id==idmin)
    		eof[index]=readFrom(college[index],currtable,index);
    		}
    	}// main loop - while
    	fclose(cancel);
    }// report
    
    int votecheck(vote curr,vote arr[],int i){
    	int j,count;
    	for(j=count=0;j<i;j++){
    		if(curr.candidate==arr[j].candidate)
    			return 1;
    		if(curr.type==1 && arr[j].type==1)
    			return 2;
    		if(curr.type==2 && arr[j].type==2)
    			count++;
    		if(count>1)	return 2;
    	}
    	return 0;
    }
    
    int readFrom(FILE *coll,vote table[],int i){
    	return fscanf(coll,"%30s%9d%1d%1d%*c",table[i].name,
    				&table[i].id,&table[i].type,&table[i].candidate);
    }	
    
    int getMin(int eof[],vote table[],int *index){
    	int i,idmin;
    	for(i=0,idmin=999999999;i<3;i++)
    		if(eof[i]!=EOF && table[i].id<idmin){
    				idmin=table[i].id;
    				*index=i;
    		}
    	return idmin;			
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Although C gets us counting from zero on up, nobody else is likely to join in. They don't say "I've got zero dogs", to mean they have 1 dog. Same with these students, the first one will be numbered from one, instead of from zero.

    Your assignments have certainly gotten longer and more involved!

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    currect represent the line of index so they are the same/

    why current.id needs to differ idmin??(its always not)

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i managed to intend this code
    what is the role
    of the red part
    and blue part?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    
     
    typedef struct vote {
    	char name [30];
    	int id,type,candidate;
    } vote;
    void report(FILE* college[3], FILE* arr[10], char* canceled);
    // Fuction returns the minimal id
    int getMin(int eof[],vote [],int *);
    // Function checks a vote record correct
    int votecheck(vote ,vote [],int i);
    // Function reads the next record from "college" file
    int readFrom(FILE *,vote [],int);
    
    
    
    int main ()
    {
    FILE* f[10];
    FILE* c[3];
    char*  name="c:\\canceled.txt";
    
    c[0]=fopen("c:\\file1.txt","r");//vote files ,each file represents the votes from a college
    c[1]=fopen("c:\\file2.txt","r");
    c[2]=fopen("c:\\file3.txt","r");
    f[0]=fopen("c:\\cfile1.txt","w");//condidate files
    f[1]=fopen("c:\\cfile2.txt","w");
    f[2]=fopen("c:\\cfile3.txt","w");
    f[3]=fopen("c:\\cfile4.txt","w");
    f[4]=fopen("c:\\cfile5.txt","w");
    f[5]=fopen("c:\\cfile6.txt","w");
    f[6]=fopen("c:\\cfile7.txt","w");
    f[7]=fopen("c:\\cfile8.txt","w");
    f[8]=fopen("c:\\cfile9.txt","w");
    f[9]=fopen("c:\\cfile10.txt","w");
    
    report(c, f, name);
    
    fclose(c[0]);
    fclose(c[1]);
    fclose(c[2]);
    fclose(f[0]);
    fclose(f[1]);
    fclose(f[2]);
    fclose(f[3]);
    fclose(f[4]);
    fclose(f[5]);
    fclose(f[6]);
    fclose(f[7]);
    fclose(f[8]);
    fclose(f[9]);
     
    
    	return 0;
    }
    void  report(FILE *college[3], FILE *arr[10], char* canceled){
    	FILE *cancel;
    	vote currtable[3], // array for three current "college" records
    		votable[3],    // array for checking the records with the same "id"
    		current;	   // struct for the current "college" record	
    	int i,j,idmin,index,flag,eof[3];
    	if(!(cancel=fopen(canceled,"w"))) exit(1);
    	
    	for(i=0;i<3;i++)
    		eof[i]= readFrom(college[i],currtable,i);
    // main loop	
    	while(eof[0]!=EOF||eof[1]!=EOF||eof[2]!=EOF)
    	{
    		idmin=getMin(eof,currtable,&index);
    		for(i=flag=0;i<3;i++)
    			if(i!=index && eof[i]!=EOF && currtable[i].id==idmin)
    				flag=3;
    		if(flag)
    		{
    		    fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			for(i=0;i<3;i++)
    				while(eof[i]!=EOF && currtable[i].id==idmin)
    				eof[i]=readFrom(college[i],currtable,i);
    			continue;
    		}
             //Filling struct table for the minimal id
    		votable[0]=currtable[index];
    		i=1;
    		while(eof[index]!=EOF && i<3)
    		{
    			eof[index]=fscanf(college[index],"%30s%9d%1d%1d%*c",current.name,&current.id,&current.type,&current.candidate);
    		    if(current.id!=idmin)
    			{
    			    for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",idmin,index,votable[j].type);
    		        currtable[index]=current;
    		        break;
    		     }
    		     else
    			 {
    			        flag=votecheck(current,votable,i);
    			        if(flag)
    			        {
      				        fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			            while(eof[index]!=EOF && currtable[index].id==idmin)
    			            eof[index]=readFrom(college[index],currtable,index);
    			            break;
    			         }
    			         else
    				     votable[i++]=current;
    		      }//else
    	      }//while
    	      if(i<3)  continue;
    		  if(eof[index]!=EOF)
    		  eof[index]=fscanf(college[index],"%30[^\n]%9d%1d%1d%*c",current.name,&current.id,&current.type,&current.candidate);
    		  if(eof[index]==EOF ||current.id!=idmin )
    		  {
    			  for(j=0;j<i;j++)
    			  fprintf(arr[votable[j].candidate],"%9d%d%d\n",idmin,index,votable[j].type);		
    			  currtable[index]=current;
     		  }
    		  else
    		  {
    		      fprintf(cancel,"%s%d%d\n",votable[0].name,idmin,2);
                  while(eof[index]!=EOF && currtable[index].id==idmin)
    		      eof[index]=readFrom(college[index],currtable,index);
    		  }
    	}// main loop - while
    	fclose(cancel);
    }// report
    
    int votecheck(vote curr,vote arr[],int i)
     {
    	int j,count;
    	for(j=count=0;j<i;j++){
    		if(curr.candidate==arr[j].candidate)
    			return 1;
    		if(curr.type==1 && arr[j].type==1)
    			return 2;
    		if(curr.type==2 && arr[j].type==2)
    			count++;
    		if(count>1)	return 2;
    	}
    	return 0;
    }
    
    int readFrom(FILE *coll,vote table[],int i)
    {
    	return fscanf(coll,"%30s%9d%1d%1d%*c",table[i].name,
    				&table[i].id,&table[i].type,&table[i].candidate);
    }	
    
    int getMin(int eof[],vote table[],int *index)
    {
    	int i,idmin;
    	for(i=0,idmin=999999999;i<3;i++)
    		if(eof[i]!=EOF && table[i].id<idmin)
    		{
    				idmin=table[i].id;
    				*index=i;
    		}
    	return idmin;			
    }

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what this part does
    Code:
    //Filling struct table for the minimal id
    		votable[0]=currtable[index];
    		i=1;
    		while(eof[index]!=EOF && i<3)
    		{
    			eof[index]=fscanf(college[index],"%30s%9d%1d%1d%*c",current.name,&current.id,&current.type,&current.candidate);
    		    if(current.id!=idmin)
    			{
    			    for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",idmin,index,votable[j].type);
    		        currtable[index]=current;
    		        break;
    		     }
    		     else
    			 {
    			        flag=votecheck(current,votable,i);
    			        if(flag)
    			        {
      				        fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			            while(eof[index]!=EOF && currtable[index].id==idmin)
    			            eof[index]=readFrom(college[index],currtable,index);
    			            break;
    			         }
    			         else
    				     votable[i++]=current;
    		      }//else
    	      }//while
    	      if(i<3)  continue;
    		  if(eof[index]!=EOF)
    		  eof[index]=fscanf(college[index],"%30[^\n]%9d%1d%1d%*c",current.name,&current.id,&current.type,&current.candidate);
    		  if(eof[index]==EOF ||current.id!=idmin )
    		  {
    			  for(j=0;j<i;j++)
    			  fprintf(arr[votable[j].candidate],"%9d%d%d\n",idmin,index,votable[j].type);		
    			  currtable[index]=current;
     		  }
    		  else
    		  {
    		      fprintf(cancel,"%s%d%d\n",votable[0].name,idmin,2);
                  while(eof[index]!=EOF && currtable[index].id==idmin)
    		      eof[index]=readFrom(college[index],currtable,index);
    		  }
    	}// main loop - while
    	fclose(cancel);
    }// report

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Skipping part of the code.
    By And3rs in forum C Programming
    Replies: 38
    Last Post: 10-01-2008, 01:52 PM
  2. Code writing issues Part 2...
    By jaybo684 in forum C++ Programming
    Replies: 10
    Last Post: 08-01-2005, 08:28 AM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM