Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,169 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,890 people online right now. Registration is fast and FREE... Join Now!




Why doesn't it compile?

 
Reply to this topicStart new topic

Why doesn't it compile?

SebKom
13 Dec, 2007 - 03:57 PM
Post #1

New D.I.C Head
*

Joined: 16 Jun, 2007
Posts: 39


My Contributions
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’

User is offlineProfile CardPM
+Quote Post

lockdown
RE: Why Doesn't It Compile?
13 Dec, 2007 - 04:02 PM
Post #2

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
What IDE were you getting errors in?
User is offlineProfile CardPM
+Quote Post

SebKom
RE: Why Doesn't It Compile?
13 Dec, 2007 - 04:04 PM
Post #3

New D.I.C Head
*

Joined: 16 Jun, 2007
Posts: 39


My Contributions
I think it's g++, I am not using Linux, the report is from a friend who does.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Why Doesn't It Compile?
13 Dec, 2007 - 05:23 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,448



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(SebKom @ 13 Dec, 2007 - 05:04 PM) *

I think it's g++, I am not using Linux, the report is from a friend who does.

What arguments are they using when they put this in? I'm assuming it's gcc at the command line.
User is online!Profile CardPM
+Quote Post

Nayana
RE: Why Doesn't It Compile?
13 Dec, 2007 - 05:48 PM
Post #5

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
From looking at your code, the compiler errors are correct. You have redeclared a whole bunch of variables.

Maybe you should comment out the subsequent declarations, or rename the other variables.

This post has been edited by Nayana: 13 Dec, 2007 - 05:51 PM
User is offlineProfile CardPM
+Quote Post

SebKom
RE: Why Doesn't It Compile?
13 Dec, 2007 - 06:08 PM
Post #6

New D.I.C Head
*

Joined: 16 Jun, 2007
Posts: 39


My Contributions
It worked fine with MingW Dev Studio on the PC. Is redeclaration wrong or just unnecessary?
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Why Doesn't It Compile?
13 Dec, 2007 - 06:30 PM
Post #7

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
<problems posting...>

This post has been edited by jjhaag: 13 Dec, 2007 - 06:34 PM
User is offlineProfile CardPM
+Quote Post

SebKom
RE: Why Doesn't It Compile?
13 Dec, 2007 - 07:28 PM
Post #8

New D.I.C Head
*

Joined: 16 Jun, 2007
Posts: 39


My Contributions
What does it mean?
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Why Doesn't It Compile?
13 Dec, 2007 - 07:45 PM
Post #9

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Sorry, for some reason I was getting a weird error when trying to post, and I couldn't get anything other than that to take in the edits.

That code actually doesn't compile on the latest gcc under MinGW. I believe that declarations like that are not allowed under the standard, though I don't have a copy handy, unfortunately. You can do things with scoping that look like redeclarations, but that's a totally different thing.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Why Doesn't It Compile?
13 Dec, 2007 - 07:48 PM
Post #10

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
You keep redeclaring variables, e.g.
CODE

double gmax=max[0];
...
double gmax=max[0]; // this one will cause an error;


You have a few options. You could use different names for the variables. You can redefine, but not redeclare, e.g.
CODE

double gmax=max[0];
...
gmax=max[0]; // this is fine.


You can put different logical blocks in different functions. Also, and this is kind of a hack, you can scope your variables so they don't mess with each other. , e.g.
CODE

{
double gmax=max[0];
...
}
// gmax doesn't even exists outside the braces.
...
{
double gmax=max[0]; // this will work, we're safe in our local scope.
...
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

SebKom
RE: Why Doesn't It Compile?
15 Dec, 2007 - 05:47 PM
Post #11

New D.I.C Head
*

Joined: 16 Jun, 2007
Posts: 39


My Contributions
Yup, it did, thanks!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:24AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month