Currently having an issue with the following code returning an array with symbols appended to the end. This is the smallest I could get the code in order to reproduce the error. What is causing this issue? I assume that maybe the array is getting numbers in it somehow which get interpreted as ascii symbols, but I can't figure out where this is happening.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int numRow = 6;
const int numCol = 26;
char letters[numRow][numCol] = {
{ 'm', 'w', 'r', 'u', 't', 'v', 'n', 'j', 'd', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'n', 'x', 'm', 'd', 'q', 'y', 'u', 't' },
{ 'y', 'e', 'r', 'y', 'e', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g', 'd', 'b', 'b', 'b', 'g', 'x', 'z' },
{ 'j', 'd', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'n', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g' },
{ 'y', 'e', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'g', 'a', 'a', 'g', 'd', 'b' },
{ 'e', 'r', 'y', 'e', 't', 't', 'v', 'n', 'j', 'd', 'j', 'y', 'k', 'w', 'r', 's', 'f', 'h', 's', 'g', 'g', 'g', 'd', 'c', 'v', 'g' },
{ 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g', 'd', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h' }
};
int main()
{
char *ltrptr;
ltrptr = &letters[0][0];
const int arraySize = 6 * 26;
int answer = 0;
cout << " Select row for sort: " << endl;
cin >> answer;
char newArray[numCol];
char *ltrptr2;
ltrptr2 = &newArray[0];
for (int i = 0; i < numCol; i++){
newArray[i] = letters[answer - 1][i];
}
cout << "Selected row: before" << newArray << endl;
selectionSort(ltrptr2, numCol, ascending);
cout << "Selected row: after " << newArray << endl;
getchar();
return 0;
}
It would be better if you actually included what output you got and what output you expected (and also the input given), rather than trying to describe the output.
But your code has an obvious error which is consistent with your vague description: you haven't put a C string in your character array, but you try to print it as if it did contain a C string.
(in particular, to store a C string in a character array, one must store the sequence of characters followed by a null character)
Related
A simple command here gave me a confusing output:
I have to create a list of a number of sequences.
For converting a fasta text file with "/n"s and titles in between (marked with ">") to a list of strings seperated only where a new title begins I tried:
x=int(0)
while x<len(my_enzyme_list):
if my_enzyme_list[x].startswith(">"):
x += 2
else:
my_enzyme_list[x-1:x] = "".join(my_enzyme_list[x-1:x])
x += 1
and it gave me (scroll till the end):
['>human_NAPRT Q6XQN6-1\n', 'MAAEQDPEARAAARPLLTDLYQATMALGYWRAGRARDAAEFELFFRRCPFGGAFALAAGLRDCVRFLRAFRLRDADVQFLASVLPPDTDPAFFEHLRALDCSEVTVRALPEGSLAFPGVPLLQVSGPLLVVQLLETPLLCLVSYASLVATNAARLRLIAGPEKRLLEMGLRRAQGPDGGLTASTYSYLGGFDSSSNVLAGQLRGVPVAGTLAHSFVTSFSGSEVPPDPMLAPAAGEGPGVDLAAKAQVWLEQVCAHLGLGVQEPHPGERAAFVAYALAFPRAFQGLLDTYSVWRSGLPNFLAVALALGELGYRAVGVRLDSGDLLQQAQEIRKVFRAAAAQFQVPWLESVLIVVSNNIDEEALARLAQEGSEVNVIGIGTSVVTCPQQPSLGGVYKLVAVGGQPRMKLTEDPEKQTLPGSKAAFRLLGSDGSPLMDMLQLAEEPVPQAGQELRVWPPGAQEPCTVRPAQVEPLLRLCLQQGQLCEPLPSLAESRALAQLSLSRLSPEHRRLRSPAQYQVVLSERLQALVNSLCAGQSP\n', '>XP_025969437.1 nicotinate phosphoribosyltransferase isoform X2 [Dromaius novaehollandiae]\n', 'M', 'A', 'L', 'L', 'T', 'D', 'L', 'Y', 'Q', 'V', 'T', 'M', 'A', 'Y', 'G', 'Y', 'W', 'R', 'A', 'G', 'R', 'H', 'R', 'V', 'P', 'A', 'A', 'A', 'E', 'L', 'F', 'F', 'R', 'R', 'C', 'P', 'F', 'R', 'G', 'A', 'F', 'A', 'L', 'G', 'A', 'G', 'L', 'A', 'E', 'G', 'L', 'R', 'F', 'L', 'R', 'A', 'F', 'R', 'F', 'S', 'A', 'A', 'D', 'I', 'A', 'Y', 'L', 'R', 'S', 'V', 'L', 'P', 'S', 'T', 'T', 'E', 'E', 'D', 'F', 'F', '\n', 'E', 'Y', 'L', 'A', 'T', 'V', 'D', 'A', 'S', 'E', 'V', 'T', 'I', 'S', 'S', 'V', 'P', 'E', 'G', 'S', 'V', 'V', 'F', 'S', 'R', 'V', 'P', 'L', 'L', 'Q', 'V', 'K', 'G', 'P', 'L', 'L', 'V', 'V', 'Q', 'L', 'L', 'E', 'T', 'T', 'L', 'L', 'C', 'L', 'V', 'S', 'Y', 'A', 'S', 'L', 'V', 'A', 'T', 'N', 'A', 'A', 'R', 'F', 'R', 'L', 'L', 'A', 'G', 'P', 'A', 'T', 'K', 'L', 'M', 'E', 'M', 'G', 'L', 'R', 'R', 'A', '\n', 'Q', 'G', 'P', 'D', 'G', 'G', 'L', 'S', 'A', 'S', 'K', 'Y', 'S', 'Y', 'I', 'G', 'G', 'F', 'D', 'C', 'T', 'S', 'N', 'V', 'L', 'A', 'G', 'K', 'L', 'Y', 'G', 'I', 'P', (and so on)
what happened?
I made a simple code that displays random characters in a 4x4 array. These unique characters are stored in arrays that are named die 1 to die 16. I show you a snippet of the code :
Here are my arrays with their names :
//Use of dice that contain unique characters
char dice1[6]={'E', 'T', 'U', 'K', 'N', 'O'};
char dice2[6]={'E', 'V', 'G', 'T', 'I', 'N'};
char dice3[6]={'D', 'E', 'C', 'A', 'M', 'P'};
char dice4[6]={'I', 'E', 'L', 'R', 'U', 'W'};
char dice5[6]={'E', 'H', 'I', 'F', 'S', 'E'};
char dice6[6]={'R', 'E', 'C', 'A', 'L', 'S'};
Here the output :
int main()
{
//init the random engine
random_device rd;
default_random_engine eng(rd());
uniform_int_distribution<int> distr(MIN, MAX);
cout << distr(eng) << endl;
//Output of a random caracetere in the form of a table
cout << dice1[distr(eng)] ;
cout << dice2[distr(eng)] ;
cout << dice3[distr(eng)] ;
cout << dice4[distr(eng)] ;
cout << endl;
cout << dice5[distr(eng)] ;
cout << dice6[distr(eng)] ;
cout << dice8[distr(eng)] ;
cout << endl;
Can we optimize this code with a loop? I thought about doing a for loop but I didn't find a convincing option to change the number at the end.
Something like this. I didn't add the extra '\n' in there if needed use a indexed for loop (I used a range based for loop):
#include <array>
#include <iostream>
#include <random>
// note do not use : using namespace std;
int main()
{
// initialization of a 2d array char dices[6][6] would also work.
std::array<std::array<char, 6>, 6> dices
{ {
{'E', 'T', 'U', 'K', 'N', 'O'},
{'E', 'V', 'G', 'T', 'I', 'N'},
{'D', 'E', 'C', 'A', 'M', 'P'},
{'I', 'E', 'L', 'R', 'U', 'W'},
{'E', 'H', 'I', 'F', 'S', 'E'},
{'R', 'E', 'C', 'A', 'L', 'S'},
} };
std::random_device rd;
std::default_random_engine eng(rd());
std::uniform_int_distribution<int> distr(0, dices[0].size()-1);
for (const auto& dice : dices)
{
std::cout << dice[distr(eng)];
}
return 0;
}
I have this nested list in my Output:
[['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'], ['O',
'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'], ['O', 'O',
'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']]
How can I bring it in this form:
[
['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'],
['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'],
['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
]
In other words: How can I make a wrap in a for-loop ?
For example:
for i in range(3):
my_list.append(i)
# How to make now a wrap ?
pprint.pprint gives a nicely formatted output:
>>> L = [['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'], ['O',
'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'], ['O', 'O',
'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']]
>>> import pprint
>>> pprint.pprint(L)
[['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'],
['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'],
['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']]
I am currently attempting to iterate through a 2d array to a function using pointer notation as an exercise. I found an example of how to do this on these forums; it's the if statement within the displayTable function. My compiler is giving me errors about the function call itself stating that the if statement from the displayTable function must be of a pointer type. Why is this not working? The example was up voted.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int numRow = 6;
const int numCol = 26;
char letters[numRow][numCol] = {
{ 'm', 'w', 'r', 'u', 't', 'v', 'n', 'j', 'd', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'n', 'x', 'm', 'd', 'q', 'y', 'u', 't' },
{ 'y', 'e', 'r', 'y', 'e', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g', 'd', 'b', 'b', 'b', 'g', 'x', 'z' },
{ 'j', 'd', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'n', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g' },
{ 'y', 'e', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'g', 'a', 'a', 'g', 'd', 'b' },
{ 'e', 'r', 'y', 'e', 't', 't', 'v', 'n', 'j', 'd', 'j', 'y', 'k', 'w', 'r', 's', 'f', 'h', 's', 'g', 'g', 'g', 'd', 'c', 'v', 'g' },
{ 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g', 'd', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h' }
};
void displayTable(char[][26]);
int main()
{
char *ltrptr = 0;
ltrptr = &letters[0][0];
const int arraySize = 6 * 26;
int answer = 0;
char * arr[6][26];
displayTable(letters);
getchar();
return 0;
}
void displayTable(char ans[][26]){
//pas 2d array, then point to it
cout << " The table as it stands: " << endl;
char * ans1;
ans1 = &ans[0][0];
for (char * iter = &ans1[0][0]; iter != &ans1[0][0] + 6 * 26; iter++){
cout << &ans1[0][0] << endl;
}
}
The problem is that your ans1 is a simple pointer-to-char and you're trying to dereference it twice. The correct way to do the loop would be
for (char * iter = ans1; iter != ans1 + 6 * 26; iter++){
cout << *iter << endl;
}
The output now is HHHHHHHHHHHHHHHHHHHH. What I would like the program to do is randomNum to have a different value each time it goes through the loop. so it would be like AacCEe... And goes on.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(){
srand(time(0));
cout << "You'r PW is: \t" << endl;
char abc [] {'A', 'a', 'B' ,'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k'};
int randomNum = rand() % 22;
for(int i = 0; i <20; i++){
cout << abc[randomNum];
}
}
You have Initialized randomNum before you loop
int randomNum = rand() % 22;
You should change your code to
int randomNum;
for(int i = 0; i <20; i++)
{
randomNum = rand() % 22;
cout << abc[randomNum];
}
This should work.
Currently randomNum gets only one value and you use that value throughout your loop without the value changing.
In the loop your printing the same character 20 times effectively, what you intended I suppose is randomly select 20 different characters. Look into my inlined comment.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(){
srand(time(0));
cout << "You'r PW is: \t" << endl;
char abc [] {'A', 'a', 'B' ,'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k'};
for(int i = 0; i <20; i++){
cout << abc[rand() % 22]; // look here
}
}