c++ - All possible sums in an array and the projection of the combinations -


well, first of newbie c++ programming, second have problem solve seems give me headache. goes this. have char[3][14] (user_in in code) , input user fill it. user types '1','x','2','*'. example:

1** 1*2 **2 *x* 1** 1** *x2 **2 **2 1x* 1** *x* **2 1** 1x2 

then transform array int[3][14] (user_in_tr in code). every * put 0, 1 put 1, x put 2, 2 put 3. so:

1**  100 1*2  103 **2  003 *x*  020 1**  100 1**  100 *x2  023 **2  003 **2  003 1x*  120 1**  100 *x*  020 **2  003 1**  100 1x2  123 

the thing want count possible sums, taking 1 element each row. notice element must not 0. wherever there 0 skip it. in example be:

first:  1+1+3+2+1+1+2+3+3+1+1+2+3+1+1=26 second: 1+1+3+2+1+1+2+3+3+1+1+2+3+1+2=27 third:  1+1+3+2+1+1+2+3+3+1+1+2+3+1+3=28 fourth: 1+1+3+2+1+1+2+3+3+2+1+2+3+1+1=27 

and on...

max sum = 42 (14x3), min sum = 14 (14x1)

also want show numbers added.
have came code results not ones expected here code:

#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <iostream>   using namespace system; using namespace std;  int main(array<system::string ^> ^args) {     char user_in[3][14];     int user_in_tr[3][14]     int i,j;     int count0,count1,count2,count3,count4,count5,count6,count7,count8,count9,count10,count11,count12,count13;     int sum;      for(i=0;i<14;i++)     {         for(j=0;j<3;j++)         {             scanf("%c",user_in[j][i]);         }     }      for(i=0;i<14;i++)     {         for(j=0;j<3;j++)         {             if(user_in[j][i]=='*')             {                 user_in_tr[j][i]=0;             }             else if(user_in[j][i]=='1')             {                 user_in_tr[j][i]=1;             }             else if(user_in[j][i]=='x')             {                 user_in_tr[j][i]=2;             }             else             {                 user_in_tr[j][i]=3;             }         }     }     for(count0=0;count0<3;count0++)    {       for(count1=0;count1<3;count1++)      {        for(count2=0;count2<3;count2++)       {         for(count3=0;count3<3;count3++)        {          for(count4=0;count4<3;count4++)         {           for(count5=0;count5<3;count5++)          {            for(count6=0;count6<3;count6++)           {             for(count7=0;count7<3;count7++)            {              for(count8=0;count8<3;count8++)             {               for(count9=0;count9<3;count9++)              {                for(count10=0;count10<3;count10++)               {                 for(count11=0;count11<3;count11++)                {                  for(count12=0;count12<3;count12++)                 {                   for(count13=0;count13<3;count13++)                  {                   sum=0;                    if(user_in[count0][0]!=0&&user_in[count1][1]!=0&&user_in[count2][2]!=0&&user_in[count3][3]!=0&&user_in[count4][4]!=0&&user_in[count5][5]!=0&&user_in[count6][6]!=0&&user_in[count7][7]!=0&&user_in[count8][8]!=0&&user_in[count9][9]!=0&&user_in[count10][10]!=0&&user_in[count11][11]!=0,user_in[count12][12]!=0&&user_in[count13][13]!=0)                   {                    sum=user_in[count0][0]+user_in[count1][1]+user_in[count2][2]+user_in[count3][3]+user_in[count4][4]+user_in[count5][5]+user_in[count6][6]+user_in[count7][7]+user_in[count8][8]+user_in[count9][9]+user_in[count10][10]+user_in[count11][11]+user_in[count12][12]+user_in[count13][13];                     printf("%d",sum);                     printf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d",user_in[count0][0],user_in[count1][1],user_in[count2][2],user_in[count3][3],user_in[count4][4],user_in[count5][5],user_in[count6][6],user_in[count7][7],user_in[count8][8],user_in[count9][9],user_in[count10][10],user_in[count11][11],user_in[count12][12],user_in[count13][13]+"\n");                   }                  }                 }                }               }              }             }            }           }          }         }        }       }      }     }     system("pause");    return 0; } 

one way might consider solving problem using recursion. although not practice, recursion on problem improves readability (only 1 loop processing data).

#include <iostream> #include <vector>  typedef std::vector<unsigned> row; typedef std::vector<row> data;  void load_user_data(data& data); void calculate_sums(unsigned value, data::const_iterator begin,         data::const_iterator end);  int main(int argc, char** argv) {     data data;      load_user_data(data);      calculate_sums(0, data.begin(), data.end());      return 0; }  void load_user_data(data& data) {     // warning: untested, should read data user     /*for (unsigned = 0; < 14; ++i)     {         row temp;         (unsigned j = 0; j < 3; ++j)         {             char input = '\0';              while (input == '\0')             {                 std::cin >> input;                 switch (input)                 {                 case '1':                     temp.push_back(1);                     break;                 case 'x':                     temp.push_back(2);                     break;                 case '2':                     temp.push_back(3);                     break;                 case '*':                     break;                 default:                     input = '\0';                 }             }         }          data.push_back(temp);     }*/      // because i'm lazy, code bellow loads data     // vector data.  if had c++11 use     // initializer lists.  if want try code above,     // comment out code bellow.      row t;      t.clear(); t.push_back(1); data.push_back(t);     t.clear(); t.push_back(1); t.push_back(3); data.push_back(t);     t.clear(); t.push_back(3); data.push_back(t);     t.clear(); t.push_back(2); data.push_back(t);     t.clear(); t.push_back(1); data.push_back(t);     t.clear(); t.push_back(1); data.push_back(t);     t.clear(); t.push_back(2); t.push_back(3); data.push_back(t);     t.clear(); t.push_back(3); data.push_back(t);     t.clear(); t.push_back(3); data.push_back(t);     t.clear(); t.push_back(1); t.push_back(2); data.push_back(t);     t.clear(); t.push_back(1); data.push_back(t);     t.clear(); t.push_back(2); data.push_back(t);     t.clear(); t.push_back(3); data.push_back(t);     t.clear(); t.push_back(1); data.push_back(t);      t.clear(); t.push_back(1); t.push_back(2); t.push_back(3);     data.push_back(t); }  void calculate_sums(unsigned value, data::const_iterator begin,         data::const_iterator end) {     if (begin == end)     {         std::cout << value << std::endl;         return;     }      (row::const_iterator entry = begin->begin();             entry != begin->end(); ++entry)     {         calculate_sums(value + *entry, begin+1, end);     } } 

if want show numbers added, replace unsigned value vector add numbers in each iteration, display sum , components. if chance add example above code.

edit: following piece of code output full sums (including both numbers , result). difference previous code storing combinations can display them latter, achieved passing std::vector (a list) function calculate_sums (previously passed on running total value). hope helps.

void calculate_sums(const row& values, data::const_iterator begin,         data::const_iterator end);  int main(int argc, char** argv) {     data data;      load_user_data(data);      row temp;     calculate_sums(temp, data.begin(), data.end());      return 0; }  void load_user_data(data& data) {     // same above }  void calculate_sums(const row& values, data::const_iterator begin,         data::const_iterator end) {     if (begin == end)     {         int value = 0;         (unsigned = 0; < values.size(); ++i)         {             value += values[i];             std::cout << values[i] << char((i < values.size()-1) ? '+' : '=');         }         std::cout << value << std::endl;         return;     }      (row::const_iterator entry = begin->begin();             entry != begin->end(); ++entry)     {         row temp(values);         temp.push_back(*entry);         calculate_sums(temp, begin+1, end);     } } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -