c++ read each 3 elements in array - c++

I am working on a project and i want to print in order each 3 elements of a string array.So if the string is "cadgfacbda" i want to be printed in the console :
**"cad gfa cbd a"**
This is the code :
string str("cadgfacbda");
for(int i = 0 ; i < 3 ; i++)
{
for(int j = i ; j < str.size() ; j +=3 )
{
cout << str[j]<<" ";
}
cout<<endl;
}
But what i get is :
c g c a
a f b
d a d

Code in one loop only:
string str("cadgfacbda");
for(int i = 0 ; i < str.size() ; i++) {
if(i && i%3==0)
cout<<" ";
cout << str[i];
}
cout<<endl;

I think it should go something like this:
string str("cadgfacbda");
for(int i = 0 ; i < str.size() ; i++)
{
cout << str[j]<<" ";
if( i % 3 == 0 )
cout<<endl;
}
This ofcourse assumes you need new line after every three elements. If you just need spaces then you can try this instead:
string str("cadgfacbda");
for(int i = 0 ; i < str.size() ; i++)
{
cout << str[j];
if( i % 3 == 0 )
cout<<" ";
}

int main()
{
typedef std::string::size_type size_type;
std::string str("cadgfacbda");
const size_type STEP_SIZE = 3;
for(size_type i = 0 ; i < str.size() ; i+=STEP_SIZE)
{
std::cout << str.substr(i, STEP_SIZE) << " ";
}
std::cout << std::endl;
return 0;
}

#include<stdio.h>
#include<string.h>
#include<string>
int main()
{
string str("cadgfacbda");
char arr[]=str.to_char();
for(int i=1;i<=strlen(arr);i++)
{
printf("%c",arr[i-1]);
if(i%3==0)
{
printf(" ");
}
}
}

This should work too:
int main()
{
std::string str = "cadgfacbda";
for (int i = 0; i < str.length()-3; i++)
{
for (int j = 0; j < 3; ++j)
{
if ((3 * i + j) < str.length())
std::cout << str[3 * i + j];
}
std::cout << " ";
}
return 0;
}

This should work :
string str("cadgfacbda");
for(int i = 0 ; i < str.size() ; i++)
{
if(i % 3 == 0 && i != 0) cout << " ";
cout << str[i];
}
cout << endl;

A bit late I suppose but you could just substring.
std::string str("cadgfacbda");
for (std::size_t i = 0; i < str.size(); i += 3) {
std::cout << str.substr(i, 3) << " ";
}
This saves you a ton of code and is imo more readable.
Live example

Related

invalid types 'char[int]' for array subscript

I'm trying to sort a string ,43546cdcANedn as an example, but when I run the code , I face this error:
main.cpp|32|error: invalid types 'char[int]' for array subscript|
void sortAndPrint(std::string param) {
char odd,even,sc,cc[param.length()];
int i = 0;
std::stack<char> letter,letters;
for(int i = 0; i > param.size(); i++){
letter.push(param[i]);
}
while(!letter.empty()) {letters.push(letter.top()); letter.pop();}
while(!letters.empty()){
if((char)letters.top()>= 'a' && (char)letters.top() <= 'z') sc[i] = letters.top();
else if (letters.top()>= 'A' && letters.top() <= 'Z' ) cc[i] = letters.top();
else if (int(letters.top()) % 2 = 0) even[i] = letters.top();
else if (int(letters.top()) % 2 != 0) odd [i] = letters.top();
letters.pop();
i++;
}
std::cout <<"Odd: ";
for(int i = 0; i > sizeof(odd); i++){cout <<odd[i] << " ";}
std::cout <<"\nEven: ";
for(int i = 0; i > sizeof(even); i++){cout <<even[i]<<" ";}
std::cout <<"\nSmall letters: ";
for(int i = 0; i > sizeof(sc); i++){cout <<sc[i]<<" ";}
std::cout <<"\nCaptial letters: ";
for(int i = 0; i > sizeof(cc); i++){cout <<cc[i]<<" ";}
}
I wrote this code , but there is a problem when i put the sngle or double qoute, it seems like i can not put char[int] or array element to compare it to another char.
So i found the answer by your help,
What i edited:
Removing the Char arrays and adding Vector
Fixing the for loop
Changing the conditions to an ASCII codes
void sortAndPrint(std::string param) {
std::vector < char > cc;
std::vector < char > sc;
std::vector < char > even;
std::vector < char > odd;
for (int i = 0; i < param.length(); i++) {
char letter = param[i];
if (int(letter) >= 97 && int(letter) <= 122) sc.push_back(letter);
else if (int(letter) >= 56 && int(letter) <= 90) cc.push_back(letter);
else if (int(letter) >= 47 && int(letter) <= 58) {
if ((int(letter)) % 2 == 0) even.push_back(letter);
else odd.push_back(letter);
}
}
std::cout << "Odd: ";
for (int i = 0; i < odd.size(); i++) std::cout << odd.at(i) << " ";
std::cout << "\nEven: ";
for (int i = 0; i < odd.size(); i++) std::cout << even.at(i) << " ";
std::cout << "\nSmall letters: ";
for (int i = 0; i < odd.size(); i++) std::cout << sc.at(i) << " ";
std::cout << "\nBig Letters: ";
for (int i = 0; i < odd.size(); i++) std::cout << cc.at(i) << " ";
}

How do this program but in reverse, pattern

so i want output like this
1
123
12345
123
1
i already make the program but it only output these, and im confused how to output the bottom triangle
1
123
12345
here's my program
#include <iostream>
using namespace std;
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
#Mojtaba's answer is a perffect extension to your approach.
However, I wanted to provide another method that is generally used in creating such strings that are formatted in a particular manner. It is common to create the entire pattern line by line and then print to the console all at once.
I have appropriately commented the code for your reference and it should be easy to understand:
#include <iostream>
#include <vector>
void pattern(int n) {
std::vector<std::string> lines; // store the first n lines to print later
int length = 2*n - 1; // length of each line
for(int i = 0; i < n; i++) {
std::string str = std::string(length, ' ');
for(int j = 1; j <= 2*i + 1; j++) {
str[n - i + j - 2] = j + '0';
// indexing can be figured by observing the pattern
}
lines.emplace_back(str);
}
for(int i = 0; i < n; i++) {
std::cout << lines[i] << std::endl;
}
for(int i = n-2; i >= 0; i--) {
std::cout << lines[i] << std::endl;
}
return;
}
int main() {
int n;
std::cin >> n;
pattern(n);
}
I added another for loop exactly like yours with different order from n-1. I modified your code to this:
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
for (i = n - 1; i >= 1; i--) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
Now it returns:
1
123
12345
123
1

image made from dots and letters c++

very basic Q as I just started with coding but I stuck at some point and have 0 ideas what to do.
I need to write code to get diamond shape made from dots and X letters, size based on a value (n) provided by the user, (3 ≤ n ≤ 80).
for example:
As I mentioned - I have almost 0 experience so all I could get is is this shape for n=6
height is ok, same as widht but unfortunately, the amount of X's and placement isn't correct :/
my code:
int h;
cerr << "Provide size of diamond: ";
cin >> h;
for (int i = 1; i <= h; i++)
{
for (int k = 1 ; k <= h-i ; k++)
{
cout << ".";
}
for (int j = 1; j <= i ; j++)
{
cout << "X";
}
cout << endl;
Thank you all good people who will help mi with this one :)
I help to draw points. I hope you, looking on my change, are able to update your code further to achive the required picture.
for (int i = 1; i <= h; i++)
{
for (int k = 1 ; k <= (h-i) / 2 ; k++)
{
cout << ".";
}
for (int j = 1; j <= i ; j++)
{
cout << "X";
}
for (int k = 1 ; k <= (h-i) / 2 ; k++)
{
cout << ".";
}
cout << endl;
}
In this kind of problems you can divide the problems in different parts. Such as for n=6 the image can be divided in 4 mirror images:
..X
.XX
XXX
then,
X..
XX.
XXX
and upside down mirror of them.
You said that you can draw the first one. I think if you give some more time you will be able to print the full image too.
But, if you have problems, here's the code for that
for (int i = 1; i <= h; i++) {
if((h-i)%2) continue;
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
for (int j = 1; j <= i ; j++) {
cout << "X";
}
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
cout << endl;
}
for (int i = (h/2)*2; i > 0; i--) {
if((h-i)%2) continue;
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
for (int j = 1; j <= i ; j++) {
cout << "X";
}
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
cout << endl;
}
Since this is tagged as a c++ question let's use std::string and three loops.
#include <iostream>
#include <string>
void print_diamond(int n)
{
int np = n / 2, nm = (n - 1) / 2;
int npl = np, nml = nm;
std::string str(n, '.');
for (int i = 0; i < nm; i++)
{
str[npl++] = 'X'; str[nml--] = 'X';
std::cout << str << std::endl;
}
for (int i = nm; i <= np; i++)
{
str[npl] = 'X'; str[nml] = 'X';
std::cout << str << std::endl;
}
for (int i = np; i < n - 1; i++)
{
str[npl--] = '.'; str[nml++] = '.';
std::cout << str << std::endl;
}
std::cout << std::endl;
}
Print all diamond for a shinier world...
int main()
{
for (int n = 3; n < 81; n++)
{
print_diamond(n);
}
}

Checking the numbers in an array and then comparing their values

Where do i fix the code in a way so that, i can check if the numbers in any row has equal values (e.g : if the matrix is 3*3 then for instance for the first row every number is 1)
#include <iostream>
using namespace std;
int main ()
{
int n;
cout<< "Kvadrat husnegtiin iremb:" <<endl ;
cin>> n;
int A[n][n];
for (int i = 0 ; i < n ; ++i )
{
for (int j = 0 ; j < n; ++j )
{
cout<< "["<< i<< "]"<< "["<< j<< "]"<< " Element"<< endl;
cin>> A[i][j] ;
}
}
for ( int i = 0 ; i < n ; ++i )
{
int B1 = A [i] [0] ;
for ( int j = 0 ; j < n; ++j )
{
if (B1 == A [i] [j] )
{
cout<< i<< "Baina"<< endl;
}
}
}
}
You can use a flag to check if there's a different value in your loop.
In example :
for (int i = 0; i < n; ++i)
{
int B1 = A[i][0];
bool IsDifferent = false;
for (int j = 0; !IsDifferent && j < n; ++j)
{
if (B1 != A[i][j]) //Notice the inverted condition
{
IsDifferent = true;
}
}
if (!IsDifferent)
cout << "Line " << i << " has equal values." << endl;
}
for ( int i = 0 ; i < n ; ++i ) {
bool all_equal = true;
for ( int j = 1 ; all_equal && j < n; ++j ) {
all_equal = A[i][j] == A[i][0];
}
if ( all_equal )
cout << "row " << i << " has equal values" << endl;
}

Print combinations of a string from left to right (not permutation) c++

Given a string, I want to print all the combinations, picking letters from left to right: Ex. Input: abcd
Output:
abc
acd
abd
bcd
ac
ad
bc
bd
cd
I can do it, but i cannot generalize it, for ex. in the string abcd, i can get all the mentioned combinations by deleting only one letter. Then I can do it also by deleting two letters and so on.
code:
name = "abcdefghi";
//Deleting 2 letters
for(int i = 1; i < name.size(); i++){
for(int k = 2; k < name.size(); k++){
for(int j = 0; j < name.size(); j++){ // PRINT ARRAY
if(j != i && j != k) cout << name[j];
}
cout << endl;
}
}
// Deleting 1 letter:
for(int i = 1; i < name.size(); i++){
for(int j = 0; j < name.size(); j++){ // PRINT ARRAY
if(j != i) cout << name[j];
}
cout << endl;
}
How can I generalize it so that I can first print the combination with 1 letter missing, then 2 letters missing, then 3, and so on...
Because if I keep going like this, to get all the combinations with the number of letters missing from 1 to n, I will need n number of for loops...
You can do it like this for example:
print_combinations(const string& name) {
if(name.size() > 1) {
for(int i = 0; i < name.size(); ++i) {
string name2 = name;
name2.erase(i, 1);
cout << name2;
}
for(int i = 0; i < name.size(); ++i) {
string name2 = name;
name2.erase(i, 1);
print_combinations(name2);
}
}
}
Currently, you can use a counter and use it as flag, something like:
void print_combinations(const std::string& s)
{
if (s.size() >= 32) {
return; // String too long
}
std::uint32_t end = 1u << s.size();
for (std::uint32_t i = 0; i != end; ++i)
{
auto j = i;
for (const auto c : s)
{
if ((j & 1) != 0) {
std::cout << c;
}
j >>= 1;
}
std::cout << std::endl;
}
}
Demo