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 8503503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:46:27+00:00 2026-06-11T01:46:27+00:00

I’m trying to solve a simple algorithm a specific way where it takes the

  • 0

I’m trying to solve a simple algorithm a specific way where it takes the current row and adds it to the top most row. I know there are plenty of ways to solve this but currently I have a text file that gets read line by line. Each line is converted to an sbyte (there’s a certain reason why I am using sbyte but it’s irrelevant to my post and I won’t mention it here) and added to a list. From there, the line is reversed and added to another list. Here’s the code I have for that first part:

        List<List<sbyte>> largeNumbers = new List<List<sbyte>>();
        List<string> total = new List<string>();
        string bigIntFile = @"C:\Users\Justin\Documents\BigNumbers.txt";


        string result;

        StreamReader streamReader = new StreamReader(bigIntFile);

        while ((result = streamReader.ReadLine()) != null)
        {
            List<sbyte> largeNumber = new List<sbyte>();
            for (int i = 0; i < result.Length; i++)
            {
                sbyte singleConvertedDigit = Convert.ToSByte(result.Substring(i, 1));
                largeNumber.Add(singleConvertedDigit);
            }
            largeNumber.Reverse();
            largeNumbers.Add(largeNumber);
        }

From there, I want to use an empty list that stores strings which I will be using later for adding my numbers. However, I want to be able to add numbers to this new list named “total”. The numbers I’ll be adding to it are not all the same length and because so, I need to check if an index exists at a certain location, if it does I’ll be adding the value I’m looking at to the number that resides in that index, if not, I need to create that index and set it’s value to 0. In trying to do so, I keep getting an IndexOutOfRange exception (obviously because that index doesn’t exist). :

        foreach (var largeNumber in largeNumbers)
        {
            int totalIndex = 0;

            foreach (var digit in largeNumber)
            {
                if (total.Count == 0)
                {
                    total[totalIndex] = digit.ToString(); //Index out of Range exception occurs here
                }
                else
                {
                    total[totalIndex] = (Convert.ToSByte(total[totalIndex]) + digit).ToString();
                }

                totalIndex ++;
            }
        }

I’m just at a loss. Any Ideas on how to check if that index exists; if it does not create it and set it’s underlying value equal to 0? This is just a fun exercise for me but I am hitting a brick wall with this lovely index portion. I’ve tried to use SingleOrDefault as well as ElementAtOrDefault but they don’t seem to be working so hot for me. Thanks in advance!

  • 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-06-11T01:46:28+00:00Added an answer on June 11, 2026 at 1:46 am

    Depending on if your result is have small number of missing elements (i.e. have more than 50% elements missing) consider simply adding 0 to the list till you reach neccessary index. You may use list of nullable items (i.e. List<int?>) instead of regular values (List<int>) if you care if item is missing or not.

    Something like (non-compiled…) sample:

    // List<long> list; int index; long value
    if (index >= list.Count) 
    {
      list.AddRange(Enumerable.Repeat(0, index-list.Count+1);
    }
    list[index] = value;
    

    If you have significant number of missing elements use Dictionary (or SortedDictionary) with (index, value) pairs.

    Dictionary<int, long> items;
    if (items.ContainsKey(index))
    { 
      items[key] = value;
    }
    else
    {
      items.Add(index, value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.