The teacher gave us (in the course) the choice of either C or C++. I started with C and my friend with C++. I'm trying to help this friend with this code because she said it is not formatting okay. The students all do the exercises in Dev-C++ on windows. I'm the only one doing it on linux and vim. The problem is that when I try to compile the c++ code I get some errors. It runs fine in Dev-C++.
$ g++ luciana_matriz.cpp -o luciana_matriz
luciana_matriz.cpp: In function ‘int main()’:
luciana_matriz.cpp:105:13: error: ‘else’ without a previous ‘if’
luciana_matriz.cpp:142:12: error: ‘stdin’ was not declared in this scope
luciana_matriz.cpp:142:17: error: ‘fflush’ was not declared in this scope
luciana_matriz.cpp:143:13: error: ‘getchar’ was not declared in this scope
Here's the code:
Code:
#include <iostream>
#include <cstdlib>
/* #include <cstring> */
/* #include <unistd.h> */
/* #include <cstdio.h> */
/* #include <stdio.h> */
using namespace std;
main() {
// exercicio 1:
int l, c, TL, TC, num;
cout<< "Informe o numero de linhas: \n";
cin >> TL;
cout<< "Informe o numero de de colunas: \n";
cin>> TC;
int M[TL][TC], N[TL][TC], Mm[TL][TC], S[TL][TC], A[TL][TC], M1[TL][TC], D[5][5], Id[4][4];
srand(time(NULL));
cout<< "\n M: \n" ;
for(l=0;l<TL; l++) {
for(c=0; c<TC; c++)
{
M[l][c]=rand()%500;
cout<< M[l][c] <<"\t";
}
cout <<"\n";
}
cout<< "\n N: \n" ;
for(l=0;l<TL; l++) {
for(c=0; c<TC; c++)
{
N[l][c]=rand()%500;
cout<< N[l][c] <<"\t";
}
cout <<"\n";
}
// exercicio 1 a:
cout<< "\n\n a) Escolha um numero para multiplicar a matriz M:\t";
cin>> num;
cout<< "\n M: \t\t\t\t Mm: \n" ;
for(l=0;l<TL; l++) {
for(c=0; c<TC; c++) {
cout << M[l][c]<<"\t";
}
cout<<"\t\t";
for(c=0; c<TC; c++) {
Mm[l][c]= num * M[l][c];
cout << Mm[l][c]<<"\t";
}
cout <<"\n\n";
}
//exercicio 1 b:
cout<< "\n\n b) Gerar Matriz S sendo a soma de M e N. \n" <<" Matriz S: \n";
for(l=0;l<TL; l++) {
for(c=0; c<TC; c++)
{
S[l][c]= M[l][c] + N[l][c];
cout<< S[l][c]<< "\t";
}
cout <<"\n";
}
// exercício 1 c:
cout<< "\n\n c) Gerar Matriz A de qualquer dimensao formada pela lei A[L,C]=(3*L-2*C). \n" <<"Matriz A: \n";
cout<< "\n A: \n" ;
for(l=0;l<TL; l++) {
for(c=0; c<TC; c++) {
A[TL][TC]=(3*l - 2*c);
cout<< A[TL][TC] <<"\t";
}
cout <<"\n";
}
//exercício 1 d
cout<< "\n\n d) Mostrar a matriz lida e criar matriz M1,trasnposta da matriz M. \n" <<"Matriz M1: \n";
cout<< "\n M1: \n" ;
for(l=0;l<TL; l++) {
for(c=0; c<TC; c++) {
M1[l][c]= M[c][l];
cout<< M1[l][c] <<"\t";
}
cout <<"\n";
}
//exercicio 1 e
cout<< "\n\n d) Montar uma matriz diagonal. \n" <<"Matriz D: \n";
cout<< "\n D: \n" ;
for(l=0; l<5; l++) {
for(c=0; c<5; c++) {
if(l==c)
D[l][c]= '1';
cout<<"\t %l" << D[l][c]<<"\n";
else
D[l][c]= '0';
cout<< "\t %c" << D[l][c];
}
}
for(l=0; l<5; l++)
{
for(c=0; c<5; c++)
{
cout<<D[l][c]<< " \t ";
}
cout<< "\n";
}
// exercício 1 f
cout<< "\n\n f) Montar uma Matriz identidade. \n" <<"Matriz Id: \n";
cout<< "\n Id: \n" ;
for(l=0;l<4; l++)
{
for(c=0; c<4; c++) {
if(l==c)
Id[l][c]=1;
else
// if(l!=c)
Id[l][c]= 0;
cout<< "\t%c" <<'%' << Id[l][c] << "\t";
}
cout << "\n";
}
cout<< "\n";
fflush(stdin);
getchar();
}
Any help would be appreciated.