Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[c++] Program wyznaczający odległość pomiędzy punktami - zawieszenie programu

Ostatnio zmodyfikowano 2014-10-24 20:30
Autor Wiadomość
stryku
» 2014-10-14 22:12:10
C/C++
int i;
cin >> i;
std::vector < int > tab( i );
P-118492
marc_xxx
» 2014-10-14 23:26:02
Albo po prostu, nie wchodząc w temat funkcji z biblioteki standardowej, można z pomocą wskaźnika zainicjować dynamicznie tablicę :)

C/C++
int i;
cin >> i;
int * tab = new int[ i ];

Oczywiście można zrobić tak jak pokazał kolega wyżej (co oczywiście jest lepszym rozwiązaniem) ale nie zapomnij o dodaniu nagłówka vector
P-118498
RobaczeQ
Temat założony przez niniejszego użytkownika
» 2014-10-15 00:57:56
C/C++
#include <iostream>
#include <cmath>

using namespace std;

int tab1;
float tab2;

void program( int p, int o, int x, int j )
{
    int l, n;
    int i;
    vector < int > tab1( i );
    int po;
    po = i *( i - 1 ) / 2;
    vector < float > tab2( po );
    for( n = 1; n < i; n++ )
    {
        float n = sqrt(( tab1[ p ] - tab1[ x ] ) *( tab1[ p ] - tab1[ x ] ) +( tab1[ p + 1 ] - tab1[ x + 1 ] ) *( tab1[ p + 1 ] - tab1[ x + 1 ] ) );
        tab2[ l ] = n;
        cout << "Odleglosc miedzy " << j << " punktem a " << o << " wynosi: " << tab2[ l ] << endl;
        o++;
        p = p + 2;
    }
}

int main()
{
    int i, p, o, x, j, g, x1, y1;
    cout << "Witaj w programie obliczajacym odleglosc miedzy punktami" << endl << "Podaj ilosc punktow: ";
    cin >> i;
    cout << endl;
    vector < int > tab1( i );
    int po;
    po = i *( i - 1 ) / 2;
    vector < float > tab2( po );
   
    if( i >= 2 )
    {
        x = 0;
        int c = 0;
        int y = 1;
        while( i > x )
        {
            x++;
            cout << "Wprowadz wspolrzedna x" << y << ": ";
            cin >> x1;
            tab1[ c ] = x1;
            c++;
            cout << "Wprowadz wspolrzedna y" << y << ": ";
            cin >> y1;
            tab1[ c ] = y1;
            c++;
            y++;
        }
        int h = 0;
        int u = 1;
        cout << endl << "Wprowadzone punkty: " << endl;
        while( 2 * i > h )
        {
            cout << "Punkt " << u << " [ " << tab1[ h ] << " ; " << tab1[ h + 1 ] << " ] " << endl;
            h = h + 2;
            u++;
        }
        p = 2;
        o = 2;
        x = 0;
        j = 1;
        for( int c = 1; c < i; c++ )
        {
            program( p, o, x, j );
            p = p + 2;
            o++;
            x = x + 2;
            j++;
        }
    }
    else
    {
        cout << "Aby wykonac pomiar potrzebne sa conajmniej dwa punkty.";
    }
    return 0;
}

F:\RobaczeQQ\p15v6.cpp||In function 'void program(int, int, int, int)':|
F:\RobaczeQQ\p15v6.cpp|13|error: 'vector' was not declared in this scope|
F:\RobaczeQQ\p15v6.cpp|13|error: expected primary-expression before 'int'|
F:\RobaczeQQ\p15v6.cpp|13|error: expected ';' before 'int'|
F:\RobaczeQQ\p15v6.cpp|16|error: expected primary-expression before 'float'|
F:\RobaczeQQ\p15v6.cpp|16|error: expected ';' before 'float'|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|20|error: invalid types 'float[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|21|error: invalid types 'float[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp||In function 'int main()':|
F:\RobaczeQQ\p15v6.cpp|33|error: 'vector' was not declared in this scope|
F:\RobaczeQQ\p15v6.cpp|33|error: expected primary-expression before 'int'|
F:\RobaczeQQ\p15v6.cpp|33|error: expected ';' before 'int'|
F:\RobaczeQQ\p15v6.cpp|36|error: expected primary-expression before 'float'|
F:\RobaczeQQ\p15v6.cpp|36|error: expected ';' before 'float'|
F:\RobaczeQQ\p15v6.cpp|48|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|52|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|61|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|61|error: invalid types 'int[int]' for array subscript|
||=== Build failed: 24 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|


dodanie int/float vector
C/C++
#include <iostream>
#include <cmath>

using namespace std;

int tab1;
float tab2;
int vector;
void program( int p, int o, int x, int j )
{
    int l, n;
    int i;
    vector < int > tab1( i );
    int po;
    po = i *( i - 1 ) / 2;
    vector < float > tab2( po );
    for( n = 1; n < i; n++ )
    {
        float n = sqrt(( tab1[ p ] - tab1[ x ] ) *( tab1[ p ] - tab1[ x ] ) +( tab1[ p + 1 ] - tab1[ x + 1 ] ) *( tab1[ p + 1 ] - tab1[ x + 1 ] ) );
        tab2[ l ] = n;
        cout << "Odleglosc miedzy " << j << " punktem a " << o << " wynosi: " << tab2[ l ] << endl;
        o++;
        p = p + 2;
    }
}

int main()
{
    int i, p, o, x, j, g, x1, y1;
    cout << "Witaj w programie obliczajacym odleglosc miedzy punktami" << endl << "Podaj ilosc punktow: ";
    cin >> i;
    cout << endl;
    vector < int > tab1( i );
    int po;
    po = i *( i - 1 ) / 2;
    vector < float > tab2( po );
   
    if( i >= 2 )
    {
        x = 0;
        int c = 0;
        int y = 1;
        while( i > x )
        {
            x++;
            cout << "Wprowadz wspolrzedna x" << y << ": ";
            cin >> x1;
            tab1[ c ] = x1;
            c++;
            cout << "Wprowadz wspolrzedna y" << y << ": ";
            cin >> y1;
            tab1[ c ] = y1;
            c++;
            y++;
        }
        int h = 0;
        int u = 1;
        cout << endl << "Wprowadzone punkty: " << endl;
        while( 2 * i > h )
        {
            cout << "Punkt " << u << " [ " << tab1[ h ] << " ; " << tab1[ h + 1 ] << " ] " << endl;
            h = h + 2;
            u++;
        }
        p = 2;
        o = 2;
        x = 0;
        j = 1;
        for( int c = 1; c < i; c++ )
        {
            program( p, o, x, j );
            p = p + 2;
            o++;
            x = x + 2;
            j++;
        }
    }
    else
    {
        cout << "Aby wykonac pomiar potrzebne sa conajmniej dwa punkty.";
    }
    return 0;
}


F:\RobaczeQQ\p15v6.cpp||In function 'void program(int, int, int, int)':|
F:\RobaczeQQ\p15v6.cpp|13|error: expected primary-expression before 'int'|
F:\RobaczeQQ\p15v6.cpp|13|error: expected ';' before 'int'|
F:\RobaczeQQ\p15v6.cpp|16|error: expected primary-expression before 'float'|
F:\RobaczeQQ\p15v6.cpp|16|error: expected ';' before 'float'|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|19|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|20|error: invalid types 'float[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|21|error: invalid types 'float[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp||In function 'int main()':|
F:\RobaczeQQ\p15v6.cpp|33|error: expected primary-expression before 'int'|
F:\RobaczeQQ\p15v6.cpp|33|error: expected ';' before 'int'|
F:\RobaczeQQ\p15v6.cpp|36|error: expected primary-expression before 'float'|
F:\RobaczeQQ\p15v6.cpp|36|error: expected ';' before 'float'|
F:\RobaczeQQ\p15v6.cpp|48|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|52|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|61|error: invalid types 'int[int]' for array subscript|
F:\RobaczeQQ\p15v6.cpp|61|error: invalid types 'int[int]' for array subscript|
||=== Build failed: 22 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|






Przepraszam, jestem zielony, jednak wydaje mi się, że porywanie na głębokie wody najbardziej będzie przydatne w rozwijaniu się :)
P-118500
michal11
» 2014-10-15 01:35:29
Przede wszystkim zapomniałeś o nagłówku.
P-118501
Memento96
» 2014-10-15 08:02:41
#include <vector>
a nie
int vector

@edit
Sprawdzałem i działa, nie używasz nigdzie zmiennej g
Nie inicjujesz i oraz  l
wygodniejszy zapis od
p = p + 2;
 to
p =+ 2;
P-118502
Jacob99
» 2014-10-15 17:20:53
p += 2;
p =+ 2 nieoznacza dodaj 2 do p, tylko przypisz p wartość +2
P-118538
stevy
» 2014-10-24 20:30:34
Może spróbuj dynamicznej alokacji pamięci?
P-119323
1 « 2 »
Poprzednia strona Strona 2 z 2