Do any of you have any idea why the following doesn't compile on a Linux compiler?
CODE
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
#include "ma0990_0708_1.h"
void cs08ssk_a1();
double pw(double x, double cs08ssk_data);
double cs08ssk_data[32];
// .. cs08ssk: any header statements of any optional functions
int main()
{
generate_data("cs08ssk", cs08ssk_data);
cs08ssk_a1();
test_this(__FILE__, 0);
return 0;
}
void cs08ssk_a1()
{
// .. cs08ssk[A]:output your login id, your 7 digit student number and your name
cout << "LoginID: cs08ssk" << endl;
cout << "Student Number: 0729549" << endl;
cout << "Name: Sevastianos-Konstantinos Komianos" << endl;
// .. cs08ssk[B]:indicate the type of each of your 8 pieces, i.e. degree 1, 2 or 3
double a[32];
for (int i=1; i<33; i++)
a[i]=cs08ssk_data[i];
int c=-3;
for (int j=-4; j<4; j++)
{
if (a[c+6] != 0)
cout << "[" << j << "," << j+1 << "] is cubic \n";
else if (a[c+5] != 0)
cout << "[" << j << "," << j+1 << "] is quadratic \n";
else if (a[c+4] != 0)
cout << "[" << j << "," << j+1 << "] is linear \n";
c=c+4;
}
// .. cs08ssk[C]:evaluate the function at 8001 points in [-4, 4]
/* After coding this part, everytime I tried to execute everything went fine but I was always getting a bug message (MingW Studio)
double x[8000];
double y[8000];
int l=0;
for (double k=-4; k<4; k=k+0.001)
{
x[l]=k;
y[l]=pw(k, cs08ssk_data);
l=l+1;
}
*/
// .. cs08ssk[D]:brute force method -- estimate and show roots
/* It doesn't compile
double x[8000];
for (double j=0; j<8000; j++)
{
if (x[j]*x[j+1] < 0)
cout << "root between" << x[j] << "and" << x[j+1];
}
*/
// .. cs08ssk[E]:brute force method -- estimate and show local maximum
int i=0;
double y[8000];
double x[8000];
double max[8000];
for (int j=0; j<8000; j++)
{
if (y[j] > y[j-1] & y[j] > y[j+1])
{
max[i]=y[j];
cout << "local maximum near " << y[j-1] << " and " << y[j+1] << endl;
i=i+1;
}
}
// .. cs08ssk[F]:brute force method -- estimate and show local minimum
int f=0;
double min[8000];
for (int j=0; j<8000; j++)
{
if (y[j] < y[j-1] & y[j] < y[j+1])
{
min[f]=y[j];
cout << "local minimum near " << y[j-1] << " and " << y[j] << endl;
f=f+1;
}
}
// .. cs08ssk[G]:brute force method -- estimate and show global maximum
double gmax=max[0];
for (int g=1; g<i; g++)
{
if (gmax < max[g])
{
gmax = max[g];
}
}
cout << "global maximum: " << gmax << "\n";
// .. cs08ssk[H]:brute force method -- estimate and show global minimum
double gmin=min[0];
for (int g=1; g<i; g++)
{
if (gmin > min[g])
{
gmin = min[g];
}
}
cout << "global minimum: " << gmin << "\n";
// .. cs08ssk[I]:exactly determine any roots of the quadratics
double a[32];
for (int i=1; i<33; i++)
a[i]=cs08ssk_data[i];
int c=-3;
for (int j=-4; j<4; j++)
{
if (a[c+5] != 0)
cout << "[" << j << "," << j+1 << "] is quadratic \n";
}
// .. cs08ssk[J]:exactly determine the root of any linears
// .. cs08ssk[K]:exactly determine any local min or max of the quadratics
// .. cs08ssk[L]:exactly determine any local min or max of the cubic
// .. cs08ssk[M]:accurately show the global minimum
double gmin=min[0];
for (int g=1; g<i; g++)
{
if (gmin > min[g])
{
gmin = min[g];
}
}
cout << "global minimum: " << gmin << "\n";
// .. cs08ssk[N]:accurately show the global maximum
double gmax=max[0];
for (int g=1; g<i; g++)
{
if (gmax < max[g])
{
gmax = max[g];
}
}
cout << "global maximum: " << gmax << "\n";
// .. cs08ssk[O]:any other statements of your choice
}
// .. cs08ssk[P] any optional functions followe]
These are the error messages but I don't really know how to correct them because it works fine with MingW Dev Studio:
QUOTE
CS08SSK_a1_20071213203340.cpp: In function ‘void cs08ssk_a1()’:
CS08SSK_a1_20071213203340.cpp:132: error: redeclaration of ‘double a [32]’
CS08SSK_a1_20071213203340.cpp:37: error: ‘double a [32]’ previously declared here
CS08SSK_a1_20071213203340.cpp:136: error: redeclaration of ‘int c’
CS08SSK_a1_20071213203340.cpp:41: error: ‘int c’ previously declared here
CS08SSK_a1_20071213203340.cpp:151: error: redeclaration of ‘double gmin’
CS08SSK_a1_20071213203340.cpp:120: error: ‘double gmin’ previously declared here
CS08SSK_a1_20071213203340.cpp:163: error: redeclaration of ‘double gmax’
CS08SSK_a1_20071213203340.cpp:108: error: ‘double gmax’ previously declared here
CS08SSK_a1_20071213203340.cpp:80: warning: unused variable ‘x’