Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

runtime error 1

Status
Not open for further replies.

vic244

Technical User
Oct 8, 2006
1
IE
Hi there,
Hoping someone can shed some light on an annoying problem I'm having with some code. I wrote the program below to perform linear interpolation between given points. Its been a while since I've written anything, so I its prolly some simple problem.

Basically the code is supposed to keep prompting the user for an angle and a dB value for -180deg to 0deg, work out the step value between them and output the data to an xls file.

The program runs fine when I use just a few points, eg..
-180.0 -62.0
-160.0 -45.0
-45.0 -20.0
0 0

But when I enter more points the program freezes with a runtime error. For the record, the full list of values I'm trying to use are...

-180.00,-62.00,
-120.00,-62.00,
-95.00,-60.00,
-45.00,-35.00,
-19.00,-35.00,
-15.00,-30.00,
-12.00,-30.00,
-10.00,-26.00,
-8.00,-22.00,
-6.00,-21.00,
-4.10,-20.00,
-3.44,-14.30,
-3.00,-10.60,
-2.50,-6.90,
-2.03,-4.36,
-1.50,-2.40,
-1.00,-0.90,
-0.52,-0.14,

Wondering if this is a memory issue? I know the code ain't what you'd call pretty...but I'd love to get it working, any help would be appreciated.

CODE-----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>


#define MAX 1800


int main(void)
{

char antenna[100];
char polarization[100];
float h[MAX], ans[MAX];
int k, counter, ten_count;
float z, t, t_new, db, db_new, db_diff, step_num, step_value, last_db;

ofstream rpedata;
rpedata.open ("rpe_print_out.xls");

printf("Welcome to the RPE interpolation program. \n");
printf("This program will use linear interpolation to expand RPE data. \n\n");
printf("What model antenna will be used?: \n");
gets(antenna);
printf("What polarization will you be using?: \n");
gets(polarization);
printf("what is the last dB value at zero degrees?: \n");
scanf("%f", &last_db);

printf("\n\nOk, this is how it wil work...find the data sheet for the Dish being used \n");
printf("When prompted, enter the degree to one decimal place and then the dB value \n");
printf("I'll go ahead and calculate the rest! :) \n \n");

printf("Please enter the starting point for the iteration process: Normally -180.0! \n");
scanf("%f", &t);
printf("Please enter the the first dB level at this starting point: Eg -60db \n");
scanf("%f", &db);

counter = 1;
ans[0] = db;


while(t != 0)
{
cout << "Please enter the next degree point: " << endl;
scanf("%f", &t_new);

cout << "Please enter the dB value for this point: " << endl;
scanf("%f", &db_new);

step_num = abs(t - t_new)*10;
t = t_new;

db_diff = db_new - db;
step_value = db_diff / step_num;

cout << "The db difference is: " << db_diff << endl;
cout << "The number of diferent iteration steps is: " << step_num << endl;
cout << "The Step value is: " << step_value << endl;


while( step_num != 0 )
{
ans[counter] = db + step_value;
db = ans[counter];
//cout << ans[counter] << endl;
step_num = step_num - 1;
counter = counter +1;
//cout << "counter @:" << counter << endl;
}

}

cout << "t=: " << t << endl;
cout << "db=: " << db << endl;

rpedata << "ANTENNA TYPE:" << "\t" << antenna << endl;
rpedata << "POLARIZATION:" << "\t" << polarization << endl << endl << endl;
rpedata << "ANGLE [Degrees]" << "\t" << "dB LEVEL" << "\t" << "dB LEVEL * 10" << endl;

//THIS IS WHERE THE PRINTING TAKES PLACE!!

z = 0;
ten_count = 10;

for(k = 0; k <= 600; k++)
{
h[k] = -180.0 + z;
rpedata << h[k] << "\t" << ans[k];

if(ten_count == 10)
{
rpedata << "\t" << ans[k] << endl;
ten_count = 0;
}

else
{
rpedata << endl;
}

ten_count = ten_count + 1;
z = z + 0.1;
}

z = 0.1;

for(k = 601; k <= 1600; k++)
{
h[k] = -120.00000 + z;
rpedata << setprecision(4) << h[k];
rpedata << "\t" << ans[k];

if(ten_count == 10)
{
rpedata << "\t" << ans[k] << endl;
ten_count = 0;
}

else
{
rpedata << endl;
}

ten_count = ten_count + 1;
z = z + 0.1;
}

z = 0.1;

for(k = 1601; k != 1800; k++)
{
if(k <= 1800 )
{
h[k] = -20.00000 + z;
rpedata << setprecision(3) << h[k];
rpedata << "\t" << ans[k];

if(ten_count == 10)
{
rpedata << "\t" << ans[k] << endl;
ten_count = 0;
}

else
{
rpedata << endl;
}

ten_count = ten_count + 1;
z = z + 0.1;
}

}

h[1800] = 0;
ans[1800] = last_db;
rpedata << h[1800] << "\t" << ans[1800] << "\t" << last_db << endl;
cout << "The last db value is: " << last_db << endl;

rpedata.close();


}
----------------------------------------------------------------
 
Please use [code][/code] tags when posting code.

Here is your code, reformatted.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>


#define MAX 1800


int main(void)
{

    char antenna[100];
    char polarization[100];
    float h[MAX], ans[MAX];
    int k, counter, ten_count;
    float z, t, t_new, db, db_new, db_diff, step_num, step_value, last_db;

    ofstream rpedata;
    rpedata.open("rpe_print_out.xls");

    printf("Welcome to the RPE interpolation program. \n");
    printf("This program will use linear interpolation to expand RPE data. \n\n");
    printf("What model antenna will be used?:  \n");
    gets(antenna);
    printf("What polarization will you be using?: \n");
    gets(polarization);
    printf("what is the last dB value at zero degrees?: \n");
    scanf("%f", &last_db);

    printf("\n\nOk, this is how it wil work...find the data sheet for the Dish being used \n");
    printf("When prompted, enter the degree to one decimal place and then the dB value \n");
    printf("I'll go ahead and calculate the rest! :) \n \n");

    printf("Please enter the starting point for the iteration process: Normally -180.0! \n");
    scanf("%f", &t);
    printf("Please enter the the first dB level at this starting point: Eg -60db \n");
    scanf("%f", &db);

    counter = 1;
    ans[0] = db;


    while (t != 0) {
        cout << "Please enter the next degree point:   " << endl;
        scanf("%f", &t_new);

        cout << "Please enter the dB value for this point:   " << endl;
        scanf("%f", &db_new);

        step_num = abs(t - t_new) * 10;
        t = t_new;

        db_diff = db_new - db;
        step_value = db_diff / step_num;

        cout << "The db difference is: " << db_diff << endl;
        cout << "The number of diferent iteration steps is: " << step_num
            << endl;
        cout << "The Step value is:   " << step_value << endl;


        while (step_num != 0) {
            ans[counter] = db + step_value;
            db = ans[counter];
            //cout << ans[counter] << endl;
            step_num = step_num - 1;
            counter = counter + 1;
            //cout << "counter @:" << counter << endl;
        }

    }

    cout << "t=:   " << t << endl;
    cout << "db=:  " << db << endl;

    rpedata << "ANTENNA TYPE:" << "\t" << antenna << endl;
    rpedata << "POLARIZATION:" << "\t" << polarization << endl << endl <<
        endl;
    rpedata << "ANGLE [Degrees]" << "\t" << "dB LEVEL" << "\t" <<
        "dB LEVEL * 10" << endl;

    //THIS IS WHERE THE PRINTING TAKES PLACE!!

    z = 0;
    ten_count = 10;

    for (k = 0; k <= 600; k++) {
        h[k] = -180.0 + z;
        rpedata << h[k] << "\t" << ans[k];

        if (ten_count == 10) {
            rpedata << "\t" << ans[k] << endl;
            ten_count = 0;
        }
        else {
            rpedata << endl;
        }

        ten_count = ten_count + 1;
        z = z + 0.1;
    }

    z = 0.1;

    for (k = 601; k <= 1600; k++) {
        h[k] = -120.00000 + z;
        rpedata << setprecision(4) << h[k];
        rpedata << "\t" << ans[k];

        if (ten_count == 10) {
            rpedata << "\t" << ans[k] << endl;
            ten_count = 0;
        }
        else {
            rpedata << endl;
        }

        ten_count = ten_count + 1;
        z = z + 0.1;
    }

    z = 0.1;

    for (k = 1601; k != 1800; k++) {
        if (k <= 1800) {
            h[k] = -20.00000 + z;
            rpedata << setprecision(3) << h[k];
            rpedata << "\t" << ans[k];

            if (ten_count == 10) {
                rpedata << "\t" << ans[k] << endl;
                ten_count = 0;
            }

            else {
                rpedata << endl;
            }

            ten_count = ten_count + 1;
            z = z + 0.1;
        }

    }

    h[1800] = 0;
    ans[1800] = last_db;
    rpedata << h[1800] << "\t" << ans[1800] << "\t" << last_db << endl;
    cout << "The last db value is:   " << last_db << endl;

    rpedata.close();


}

The first problem is the strange mix of C and C++ in the same program (all those printf/scanf calls for example).
> gets(antenna);
Is especially bad -
> #include <iostream.h>
These are old-style C++ headers. The new form should be
Code:
#include <iostream>  // standard headers do not have .h
using namespace std;

Another point is that you only have main(), and it's like 150 lines long. A few functions would certainly help you focus on specific issues (and allow incremental testing).

> h[1800] = 0;
> ans[1800] = last_db;
Your arrays are also 1800 elements long, so these array accesses step off the end.
This will cause you problems.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top