Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5948493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:06:55+00:00 2026-05-22T17:06:55+00:00

This could be considered a homework question. This problem is wel known: you have

  • 0

This could be considered a homework question.
This problem is wel known: “you have a triangle of numbers and you have to find the greatest sum“

well no problem, I’ve made a solution in python some time ago, works flawless.
But now in c++, the solution is 75256, my answer is 9729.
So the problem is that the type short overflows.

So to fix this, I assumed changing my array to type int would solve everything.. but then, when declaring an array a[1001][1001] it freezes (i guess memory error).

anyone know what to do?
I tried something with another int, and whenever the value in a got bigger than 32767 it would increment, but my solution then still is off 300? (the code works – tested on many smaller ones)

#include <iostream>
#include <fstream>

int main() {
    std::ofstream fout ("numtri.out");
    std::ifstream fin  ("numtri.in");
    short trifield[1001][1001] = {0};
    int Rows, tmp=0;
    fin >> Rows;
    for (int x = 0; x<Rows;x++) 
        for (int nr = 0; nr<=x;nr++){
            fin >> tmp;
            trifield[x][nr] = tmp;}

    for (int y = (Rows-2); y > -1; y--)
        for (int x = 0; x <= y+1; x++) {
            int a = trifield[y+1][x];
            int b = trifield[y+1][x+1];
            if (a > b) trifield[y][x] += a;
            else       trifield[y][x] += b;
        }
    fout << trifield[0][0] << std::endl;
    return 0;    
}

note: I’m not looking for the solution, just a good way to deal with overflows, examples appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:06:55+00:00Added an answer on May 22, 2026 at 5:06 pm

    You get a stack overflow instead of a numeric overflow!

    Move the array to static memory outside of main, so it doesn’t use the stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be considered a duplicate question, as a similar one has already been
This could be considered a continuation of this earlier SO question . Ideally, I'd
This question is on my CS homework and I have no idea how to
This could be weird, Have you ever come across a blog which you wanted
This could quite possibly be the dumbest question ever asked. Our client wants us
This could save me so much time. Sometimes I find myself writing things like
I'm new to ASP.NET MVC so this could have an obvious answer. Right now
I have searched through google and SO for possible answers to this question, but
A recent homework assignment I have received asks us to take expressions which could
I have a homework assignment, and i am finished other then one question (see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.