Hi all, I am doing the assignment as below. It is nothing wrong but just I think I complied c code and c++ code in the program.

Now I would like to change it to work under c complier, but just dont know which part is wrong. Can anyone of you help to highlight the issue for me. Thank you.

#include<stdio.h>		//Header file
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<math.h>

//using namespace std;

#define MAXROWS 10		//Define max rows =10		
#define MAXCOLS 10		//Define max cols =10

void readinput(int in[][MAXCOLS]);		//Function prototype	
void writeoutputA(int out[][MAXCOLS]);	
void writeoutputB(int out[][MAXCOLS], int count);

void main()
{
		int random_matrix[MAXROWS][MAXCOLS];
		int tmp_matrix[MAXROWS][MAXCOLS];
		int i,j;
		int count_prime = 0;

		srand((unsigned)(time(NULL)));	/* initialize random seed: */

		printf("Below is the generated random matrix: \n");

		printf("%d\n",MAXROWS);
		printf("%d\n",MAXCOLS);

		  for (i=0;i<MAXROWS;i++)			  /* generate random number: */
		  {
			  for (j=0;j<MAXCOLS;j++)
			  {
			  random_matrix[i][j]= rand() % 100;
			  printf("%d\t",random_matrix[i][j]); /* display the generated matrix */
			  }
			  
		  }
		  /* call writeoutputA to write the display matrix into MatrixA.txt */
		  {
			  writeoutputA(random_matrix);		  
		  }				
		  /* call function readinput, to read in matrix from MatrixA.txt */
		  {
			  readinput(tmp_matrix);				
		  }
		  /* check prime number, and repleace them to 0 */
		  for (i=0;i<MAXROWS;i++)
		  {
			  for (j=0;j<MAXCOLS;j++)
			  {
				  bool prime = true;
				  if (tmp_matrix[i][j] > 2)
				  {
					  for (int num1 = 2; num1 <= tmp_matrix[i][j]-1; num1++)
					  {
						  if (tmp_matrix[i][j] % num1 == 0)
						  {
							  prime = false;
							  break;
						  }
					  }
					  if (prime == true)
					  {
						  count_prime ++;
						  tmp_matrix[i][j] = 0;
					  }
				  }  
			  }
		  }		  /* end of checking prime number*/

		  /* call function, to output the new matrix to OutputMatrix.txt */
		  {
			  writeoutputB(tmp_matrix,count_prime);			
		  }

		  system("pause"); 

}
	void writeoutputA(int out[][MAXCOLS])//Function write output to file
		{

			int row,col;					
			
			FILE *outputFile=fopen("MatrixA.txt","w");	//Declare a file pointer and open for writing	
			fprintf(outputFile,"%d\n",MAXROWS);
			fprintf(outputFile,"%d\n",MAXCOLS);
							
			for(row=0; row<MAXROWS; row++)
				{
				for(col=0; col<MAXCOLS ;col++)
				{							
					fprintf(outputFile,"%d\t", out[row][col]);									
				}							
				fprintf(outputFile,"\n");																
			}													
			fclose(outputFile);
			return;	
	}

	void writeoutputB(int out[][MAXCOLS], int count)//Function write output to file
		{

			int row,col;					

			FILE *outputFile=fopen("OutputMatrix.txt","w");	//Declare a file pointer and open for writing
			fprintf(outputFile,"%d\n",MAXROWS);
			fprintf(outputFile,"%d\n",MAXCOLS);
							
			for(row=0; row<MAXROWS; row++)
			{
				for(col=0; col<MAXCOLS ;col++)
				{							
					fprintf(outputFile,"%d\t", out[row][col]);									
				}							
				fprintf(outputFile,"\n");																
			}	
			fprintf(outputFile,"\nNumber of prime numbers is : %d\n", count);	/* print the result into OutputMatrix.txt */

			fclose(outputFile);
			return;	
	}

void readinput(int in[][MAXCOLS])
	{
		int row ,col;		//Declare row,col, ip as int type 
			
		FILE *inputFile=fopen("MatrixA.txt","r");		/*Open MatA.txt for reading*/
		if (inputFile == 0)
			{
				return;
			}
		for (int i=0;i<2;i++)
			{
				fscanf(inputFile, "%*d");
			}
		for(row=0; row<MAXROWS; row++)	/* to keep on reading every number */
			{   
				for(col=0; col<MAXCOLS; col++)			
				{
					fscanf(inputFile,"%d",&in[row][col]);
				}
			}
		fclose(inputFile); /*Close the file stream*/
	}

The biggest problem I have is just trying to read the code with the excessive and inconsistent indentation. 4 SPACEs rather than 2 TABs for each indent level is enough.

As for C++, remove #include<iostream> and if there are any new errors, that's C++ code. Change it accordingly. If you then compile it with a .C extension, C++ code will be flagged as errors.

There is no any problem, you can have mixed code C/C++ (printf,cout eg.)
depent on your compiler, if you use VS then you are ok but if you use that fail Dev-CPP how it called then if you create C project and use C++ parts then you will have problem in Compiling.

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