perimeter of triangle

Discussion in 'C++' started by NJpixie@gmail.com, Nov 19, 2005.

  1. Guest

    Hello,


    Here is my code of a perimeter of a triangle such as:
    2
    1 7
    3 6 8
    4 2 5 3

    #include <iostream>

    using namespace std;

    int perimeterSum (int[][]A, int Size);

    int main()

    }

    int perimeterSum(int[][]A, int size){
    int sum = 0;

    // the left-hand side (the first column)

    for (int i = 0; i < size; i++)
    sum += A[0];

    // the bottom row, except the first element

    for (int i = 1; i < size; i++)
    sum += A[A - 1];

    // the diagonal except the first and last elements

    for (int i = 1; i < size - 1; i++)
    sum += A;

    return sum;
    }
    }


    i am only on chapter 3 so the code needs to be very basic using:
    int perimeterSum (int[][] A, int Size) {
    int sum = 0;
    I tried this code but i got 4 errors in visual c++.

    The errors are:
    C:\Documents and Settings\perimeter.cpp(6) : error C2087: '<Unknown>' :
    missing subscript

    C:\Documents and Settings\perimeter.cpp(6) : error C2146: syntax error
    : missing ')' before identifier 'A'

    C:\Documents and Settings\perimeter.cpp(6) : error C2146: syntax error
    : missing ';' before identifier 'A'

    C:\Documents and Settings\perimeter.cpp(6) : fatal error C1004:
    unexpected end of file found
    Error executing cl.exe.

    What am I doing wrong?
     
    , Nov 19, 2005
    #1
    1. Advertising

  2. You have to give at least one of the arrays a size in the formal parameter
    in the signature of perimeterSum. And this size needs to be a constant.
    Try to figure out which one it is and why.

    On 19 Nov 2005 wrote:

    > Hello,
    >
    >
    > Here is my code of a perimeter of a triangle such as:
    > 2
    > 1 7
    > 3 6 8
    > 4 2 5 3
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int perimeterSum (int[][]A, int Size);
    >
    > int main()
    >
    > }
    >
    > int perimeterSum(int[][]A, int size){
    > int sum = 0;
    >
    > // the left-hand side (the first column)
    >
    > for (int i = 0; i < size; i++)
    > sum += A[0];
    >
    > // the bottom row, except the first element
    >
    > for (int i = 1; i < size; i++)
    > sum += A[A - 1];
    >
    > // the diagonal except the first and last elements
    >
    > for (int i = 1; i < size - 1; i++)
    > sum += A;
    >
    > return sum;
    > }
    > }
    >
    >
    > i am only on chapter 3 so the code needs to be very basic using:
    > int perimeterSum (int[][] A, int Size) {
    > int sum = 0;
    > I tried this code but i got 4 errors in visual c++.
    >
    > The errors are:
    > C:\Documents and Settings\perimeter.cpp(6) : error C2087: '<Unknown>' :
    > missing subscript
    >
    > C:\Documents and Settings\perimeter.cpp(6) : error C2146: syntax error
    > : missing ')' before identifier 'A'
    >
    > C:\Documents and Settings\perimeter.cpp(6) : error C2146: syntax error
    > : missing ';' before identifier 'A'
    >
    > C:\Documents and Settings\perimeter.cpp(6) : fatal error C1004:
    > unexpected end of file found
    > Error executing cl.exe.
    >
    > What am I doing wrong?
    >
    >
     
    Jason Barrett Prado, Nov 19, 2005
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    > Hello,
    >
    >
    > Here is my code of a perimeter of a triangle such as:
    > 2
    > 1 7
    > 3 6 8
    > 4 2 5 3
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int perimeterSum (int[][]A, int Size);
    >
    > int main()
    >
    > }
    >
    > int perimeterSum(int[][]A, int size){


    Problem here. int[][]A says it's a 2 dimentional array, but C++ has no idea
    what size it is because you didn't tell it. So how is it suppose to figure
    out which memory address [2][2] is supposed to point to? It knows normally
    by: row*TotalColumns + Column. So obvoiusly you have to let C know what teh
    total columns is at least.

    Is this a 4x4 array? then let it know.
     
    Jim Langston, Nov 20, 2005
    #3
  4. Howard Guest

    <> wrote in message
    news:...
    > Hello,
    >
    >
    > Here is my code of a perimeter of a triangle such as:
    > 2
    > 1 7
    > 3 6 8
    > 4 2 5 3
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int perimeterSum (int[][]A, int Size);
    >


    You can't leave both dimensions unspecified. You have to specify at least
    the first dimension (or use pointers).

    > int main()
    >
    > }


    Is this really your code? You open a function with the _left_ curly
    bracket: {, not }.

    >


    Is this function _inside_ your main function? Why? It should be declared
    either before or after your main function. (After is ok, because you gave
    it a prototype above.)

    > int perimeterSum(int[][]A, int size){
    > int sum = 0;
    >
    > // the left-hand side (the first column)
    >
    > for (int i = 0; i < size; i++)
    > sum += A[0];
    >
    > // the bottom row, except the first element
    >
    > for (int i = 1; i < size; i++)
    > sum += A[A - 1];
    >
    > // the diagonal except the first and last elements
    >
    > for (int i = 1; i < size - 1; i++)
    > sum += A;
    >
    > return sum;
    > }
    > }
    >
    >
    > i am only on chapter 3 so the code needs to be very basic using:
    > int perimeterSum (int[][] A, int Size) {
    > int sum = 0;
    > I tried this code but i got 4 errors in visual c++.
    >
    > The errors are:
    > C:\Documents and Settings\perimeter.cpp(6) : error C2087: '<Unknown>' :
    > missing subscript
    >
    > C:\Documents and Settings\perimeter.cpp(6) : error C2146: syntax error
    > : missing ')' before identifier 'A'
    >
    > C:\Documents and Settings\perimeter.cpp(6) : error C2146: syntax error
    > : missing ';' before identifier 'A'
    >
    > C:\Documents and Settings\perimeter.cpp(6) : fatal error C1004:
    > unexpected end of file found
    > Error executing cl.exe.
    >
    > What am I doing wrong?
    >


    It would also help if you let us know exactly what line 6 is, by commenting
    it in the code you post.

    -Howard
     
    Howard, Nov 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Johnson Jr.
    Replies:
    2
    Views:
    445
    Eric Veltman
    Dec 11, 2003
  2. ad

    No triangle in DropDownList

    ad, Jun 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    393
    Eliyahu Goldin
    Jun 5, 2005
  3. Replies:
    0
    Views:
    297
  4. Replies:
    1
    Views:
    345
  5. Replies:
    0
    Views:
    327
Loading...

Share This Page