Thread: please help

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by ali_1234 View Post
    if i use return one after fscanf then its reading only the fist number from input file and after i press enter the program terminates...
    i m using borland as my compiler
    what are you talking about?

    fscanf reads from the "indata.txt"

    what it has to do with your pressing enter?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    i am getting an output file now but the problem is min value is always zero and max mum value is some number...0814...
    now again im stuck ...

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by ali_1234 View Post
    i am getting an output file now but the problem is min value is always zero and max mum value is some number...0814...
    now again im stuck ...
    post your last code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int i,a[10],val,n=0,max,min;
    	FILE *fi,*fo;
    	if((fi=fopen("indata.txt","r"))==NULL)
    	{
    		printf("Error in opening the file!!!\n");
    		return 1;
    	}
    	
    	else
    	{
    		while(fscanf(fi,"%d",&val))
    		{
    			a[n]=val;
    			n=(n+1);
             printf("%d %d\n", val, n);
             getch ();
    
    
         
    		max = a[0];
    		min = a[0];
    		
    		for (i = 0; i <= 10; i++)
    		{
    			if (a[i] > max)
    			{
    				max = a[i];
    			}
    			else if (a[i] < min)
    			{
    				min = a[i];
    			}
          }
    		if((fo=fopen("result.txt","w"))==NULL)
    		{
    			printf("Error in opening the file!!!\n");
    			return 1;
    		}
    		else
    		{
    			fprintf (fo,"Maximum element in an array : %d\n", max);
    			fprintf (fo,"Minimum element in an array : %d\n", min);
    		}
    		
    		fclose(fo);
    		fclose(fi);
    		getchar ();
    		return 0;
    	   }
             }
    }

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    your indentetion suxx big deal - you do not even see simplest bugs

    also - when you have return inside if - you do not need else - it just adds unneded indentation level

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int i,a[10],val,n=0,max,min;
    	FILE *fi,*fo;
    	if((fi=fopen("indata.txt","r"))==NULL)
    	{
    		printf("Error in opening the file!!!\n");
    		return 1;
    	}
    
    	while(fscanf(fi,"%d",&val) == 1)
    	{
    		a[n]=val;
    		n=(n+1);
    		printf("%d %d\n", val, n);
    		if(n == 10)
    			break;
    	}
    
    
    	max = a[0];
    	min = a[0];
    
    	for (i = 0; i <= 10; i++)
    	{
    		if (a[i] > max)
    		{
    			max = a[i];
    		}
    		else if (a[i] < min)
    		{
    			min = a[i];
    		}
    	}
    	if((fo=fopen("result.txt","w"))==NULL)
    	{
    		printf("Error in opening the file!!!\n");
    		return 1;
    	}
    	fprintf (fo,"Maximum element in an array : %d\n", max);
    	fprintf (fo,"Minimum element in an array : %d\n", min);
    
    	fclose(fo);
    	fclose(fi);
    	getchar ();
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    thanks people for ur kind support ...
    and vart i am really sorry i am jus a beginner and my programming teacher sux .. so i dont know a damn about it .. wht evr i have studied is by my self ... i m really thank full for ur support cudn't do this without u ppl !!!..

    THANK YOU PPL !!!!........... spl VART !!

  7. #22
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by ali_1234 View Post
    thanks people for ur kind support ...
    and vart i am really sorry i am jus a beginner and my programming teacher sux .. so i dont know a damn about it .. wht evr i have studied is by my self ... i m really thank full for ur support cudn't do this without u ppl !!!..

    THANK YOU PPL !!!!........... spl VART !!
    BTW - you still have a bug in your for loop - you access 11 elements whule have only 10 in the array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Tags for this Thread