Answer to Question #260888 in C++ for c++readytocode

Question #260888

This program will process a user-selected txt file. The only valid file names are input1.txt and input2.txt both of which are automatically included in the Mimir project. The program will provide some interesting statistics about the numbers in that file. The file itself contains a bunch of integers organized into an unknown number of rows with an unknown number of integers per row. *NOTE: Be sure to download the two files above for testing in your CLion project.

Use the output below as a guide for the code you need to write. the [filename] will not show in mimir but will in Clion.

Good input

Enter the name of the file to process: [input1.txt]


Count: 205

Odd: 110

Even: 95

Rows: 22

Average row size: 9

Min: 0

Max: 97



<no using sstream!!!>


1
Expert's answer
2021-11-04T16:26:19-0400
#include <limits.h>
#include <string.h>
#include <iostream>
#include <ctype.h>
#include<string.h>


#define INPUT1 (1)


#define INPUT2 (2)


using namespace std;










int file(char * name)


{


    int i_return=-1;


		


	printf(" Enter the file name:  ");


	scanf("%s",&name);


	


	int n = strlen(name);


	for (int i=0; i<n ;i++)


	{


	   char c = name[i];


	   name[i] = toupper(c);	


	}	


	


     if (strstr(name,"INPUT1.TXT")!=NULL)


     {


     	i_return=INPUT1;


	 }


     if (strstr(name,"INPUT2.TXT")!=NULL)


     {


     	i_return=INPUT2;


	 }


	 return 0;


	


}






void processingFile(FILE * file)


{


	int min_val = INT_MAX; 


	int max_val = INT_MIN; 


	int rowCount = 0;


	int k=0;


	int num_rows=0;


	int num_odd=0;


	int num_even=0;


	


	


	while (!feof(file))


	{


	    char buffer[1024];


	    if (fgets(buffer,1024,file) !=NULL)


	    {


	         num_rows++;


	         int current_row_count=0;


	         int x;


	         char * t;


	         char * buff_pointer = buffer;


			  


	         while ((t = strtok(buff_pointer," \t"))!=NULL)


	         {


			    x = to_string(token);


	         


	            if (x<min_val) { min_val = iNum; }


				if (x>max_val) { max_val = iNum; }


				current_row_count++;


				k++;


				


				if (x%2==0) { num_even++; } else { num_odd++;	}


				buff_pointer=NULL;


				


		     }   


			  


	        rowCount += current_row_count;         


		} 


	


	} 


	


	printf(" Count: %d \n",k);


	printf(" Odd: %d \n",num_odd);


	printf(" Even: %d \n",num_even);


	printf(" Rows: %d \n",num_rows);


	printf(" Average row size : %d \n",rowCount/num_rows);


	printf(" min: %d \n",min_val);


	printf(" max: %d \n",max_Val);


	


}














Go()


{


	


	char name[255];


	int start = file(name);


	 FILE * f;


	 


	switch (start)


	{


		case 1:


		case 2:


		{


			


            f = fopen(name,"r");	


            if (f!=NULL)


            {


			  processingFile(f);		


			  fclose(f);


		    }


			break;


		}


		


		default:


		{


			printf(" The file does not exist \n");


		    return -1;


		    break;		


		}


	 } 


}














main() { Go(); }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS