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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T10:59:23+00:00 2026-05-28T10:59:23+00:00

I have a string in c++ and it represents an upper triangular matrix, What

  • 0

I have a string in c++ and it represents an upper triangular matrix, What I want to do is meake a complete matrix from this string

    std::string input = "1,2,1,3,6,1,4,7,9,1";
    //this represents


 //1  2  3 4
 //2  1  6 7
 //3  6  1 9
 //4  7  9 1

std::replace(input.begin(), input.end(), ',', ' ');
std::vector<double> Matrix;
std::istringstream inputStream(input);
double value;
int rowNum = 0;
int colNum = 0; 

while (inputStream >> value){
    for (colNum = 0; colNum < 2; colNum++){
        if (colNum >= rowNum){
            Matrix.push_back( value ); 
        }
        else{
            Matrix.push_back( Matrix[colNum * 2 + rowNum]); 
        }
    }

    rowNum++;
}
inputStream >> std::ws;

Instead of getting

 1  2  3 4
 2  1  6 7
 3  6  1 9
 4  7  9 1

But I am getting

   1.0000   1.0000   1.0000   2.0000
   1.0000   1.0000   2.0000   1.0000
   1.0000   2.0000   1.0000   1.0000
   2.0000   1.0000   1.0000   2.0000

What is it my error? I can not see it…

  • 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-28T10:59:23+00:00Added an answer on May 28, 2026 at 10:59 am

    You should show the indexing scheme used for printing the output (i.e. how do you expect the indexes works): your choice of using a vector instead of a matrix make hard to correct the code. For sure, I see the following points that have no clear connection with the input pattern:

    1) each number you read you increment the rowNum index. The row should be incremented instead at ‘steps’ 1, 1+2, 1+2+3,…

    2) colNum should range from 0 to current rowNum, instead assumes just 0,1

    3) there is no chance to fill a row (say the first) before you read (say the last). You could do if the input would be 1 2 3 4 1 6 7 1 9 1

    all these points are related, and origin from the wrong data representation, that make difficult a trivial task.

    In C++, a very effective way to tackle these problems is data hiding: consider how easily we can write a class that gives the correct logical representation and doesn’t waste space:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    template <class T = double>
    class upper_triangular_matrix
    {
        std::vector<T> Matrix;
    
    public:
    
        upper_triangular_matrix(std::string input)
        {
            // trade time for space: store the values, compute indexing
            std::replace(input.begin(), input.end(), ',', ' ');
            std::istringstream inputStream(input);
            T value;
            while (inputStream >> value)
                Matrix.push_back(value);
    
            // validate size: ok 1,1+2,1+2+3 etc
        }
    
        T operator()(int r, int c) const
        {
            // compute indexing accounting for miss duplicated
            if (c > r)
                std::swap(c, r);
            int p = 0, n = 1;
            while (r > 0)
            {
                p += n++;
                r--;
            }
            return Matrix[p + c];
        }
    };
    
    int main()
    {
        upper_triangular_matrix<> m("1,2,1,3,6,1,4,7,9,1");
        for (int r = 0; r < 4; ++r)
        {
            for (int c = 0; c < 4; ++c)
                std::cout << m(r, c) << ' ';
            std::cout << std::endl;
        }
    }
    

    when run, this prints

    1 2 3 4 
    2 1 6 7 
    3 6 1 9 
    4 7 9 1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string and I want to check if it represents a proper
I have a data structure that represents C# code like this: class Namespace: string
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have this string that represents the body of a page, which I would
I have a string that represents an html document. I'm trying to replace text
Lets say I have a string that represents a date that looks like this:
I have a dilemma that goes like this. I have a string that represents
I have a string that represents a path to a directory. I want split
Here I have string like this 1322485986.672901000 , data type is string which represents
I have a string which represents a DataTime value, and I want to workout

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.