{"id":27670,"date":"2022-12-25T18:31:40","date_gmt":"2022-12-25T13:01:40","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/"},"modified":"2022-12-25T18:31:40","modified_gmt":"2022-12-25T13:01:40","slug":"solved-text-file-into-array-c-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/","title":{"rendered":"[Solved] text file into array C++ [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-43312741\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"43312741\" data-parentid=\"43309127\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Here&#8217;s a simple way to make the pattern you want from the data you have. I&#8217;m going to use an array rather than reading from a file, but it shouldn&#8217;t be hard to do that if you want to.<\/p>\n<p>Basically, the trick is to look at the index of each item at each position in the output. If, instead of outputting the actual value you want, you look at the index of the value in the array, it looks like this:<\/p>\n<pre><code>5 5 5 5 5 5 5 5 5 5 5 \n5 4 4 4 4 4 4 4 4 4 5 \n5 4 3 3 3 3 3 3 3 4 5 \n5 4 3 2 2 2 2 2 3 4 5 \n5 4 3 2 1 1 1 2 3 4 5 \n5 4 3 2 1 0 1 2 3 4 5 \n5 4 3 2 1 1 1 2 3 4 5 \n5 4 3 2 2 2 2 2 3 4 5 \n5 4 3 3 3 3 3 3 3 4 5 \n5 4 4 4 4 4 4 4 4 4 5 \n5 5 5 5 5 5 5 5 5 5 5 \n<\/code><\/pre>\n<p>So now, think about what the coordinates of each position are. If we write it out as the coordinates, it looks like this:<\/p>\n<pre><code>&lt;0,0&gt; &lt;1,0&gt; &lt;2,0&gt; &lt;3,0&gt; &lt;4,0&gt; ... etc.\n&lt;0,1&gt; &lt;1,1&gt; &lt;2,1&gt; &lt;3,1&gt; &lt;4,1&gt; ... etc.\n&lt;0,2&gt; &lt;1,2&gt; &lt;2,2&gt; &lt;3,2&gt; &lt;4,2&gt; ... etc.\n&lt;0,3&gt; &lt;1,3&gt; &lt;2,3&gt; &lt;3,3&gt; &lt;4,3&gt; ... etc.\n... etc.\n<\/code><\/pre>\n<p>So how do those coordinates match up with the indexes? We obviously want the relationship to have something to do with their distances from the center of the square. So let&#8217;s subtract out the center coordinate from all the other coordinates:<\/p>\n<pre><code>&lt;-5,-5&gt; &lt;-4,-5&gt; &lt;-3,-5&gt; &lt;-2,-5&gt; ... etc.\n&lt;-5,-4&gt; &lt;-4,-4&gt; &lt;-3,-4&gt; &lt;-2,-4&gt; ... etc.\n&lt;-5,-3&gt; &lt;-4,-3&gt; &lt;-3,-3&gt; &lt;-2,-3&gt; ... etc.\n&lt;-5,-2&gt; &lt;-4,-2&gt; &lt;-3,-2&gt; &lt;-2,-2&gt; ... etc.\n...etc. \n<\/code><\/pre>\n<p>So what&#8217;s the relationship? Notice how all along the top and left edge, there&#8217;s a <code>-5<\/code>? And we want our outermost result to be <code>5<\/code>. And in all of the 2nd row and 2nd column, there&#8217;s a <code>-4<\/code>. We want the rest of those values to be a <code>4<\/code>. So a pattern&#8217;s emerging here. It looks like you need to take the maximum of the absolute value of the coordinate to figure out the index. Then use the index to get the value to put there.<\/p>\n<p>So putting it all together, it would look something like this (I substituted reading from a file for just using an array, but you can build the array from the file) :<\/p>\n<pre><code>    int distances[] = { 9, 5, 4, 3, 2, 1 };\n    const int centerX   = 5;\n    const int centerY   = 5;\n\n    for (int x = 0; x &lt; 11; x++)\n    {\n        for (int y = 0; y &lt; 11; y++)\n        {\n            int largest = std::max (abs(x - centerX), abs(y - centerY));\n            std::cout &lt;&lt; distances [ largest ] &lt;&lt; \" \";\n        }\n\n        std::cout &lt;&lt; \"\\n\";\n    }\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">4<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved text file into array C++ [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Here&#8217;s a simple way to make the pattern you want from the data you have. I&#8217;m going to use an array rather than reading from a file, but it shouldn&#8217;t be hard to do that if you want to. Basically, the trick is to look at the index of each item at each position &#8230; <a title=\"[Solved] text file into array C++ [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/\" aria-label=\"More on [Solved] text file into array C++ [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-27670","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] text file into array C++ [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] text file into array C++ [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Here&#8217;s a simple way to make the pattern you want from the data you have. I&#8217;m going to use an array rather than reading from a file, but it shouldn&#8217;t be hard to do that if you want to. Basically, the trick is to look at the index of each item at each position ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-25T13:01:40+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] text file into array C++ [closed]\",\"datePublished\":\"2022-12-25T13:01:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/\"},\"wordCount\":282,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/\",\"name\":\"[Solved] text file into array C++ [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-25T13:01:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] text file into array C++ [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] text file into array C++ [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] text file into array C++ [closed] - JassWeb","og_description":"[ad_1] Here&#8217;s a simple way to make the pattern you want from the data you have. I&#8217;m going to use an array rather than reading from a file, but it shouldn&#8217;t be hard to do that if you want to. Basically, the trick is to look at the index of each item at each position ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/","og_site_name":"JassWeb","article_published_time":"2022-12-25T13:01:40+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] text file into array C++ [closed]","datePublished":"2022-12-25T13:01:40+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/"},"wordCount":282,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/","name":"[Solved] text file into array C++ [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-25T13:01:40+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-text-file-into-array-c-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] text file into array C++ [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=27670"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27670\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=27670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=27670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=27670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}