Write a program which generate random words (number of words = n). Max length of word = m.
Words must contain big and samll letters. Probability of big letters must eqals 50%.
Example:
Input: 2 4
Output: AbCd eFgH
How do I do that?
So far i figured out how to generate random small and big letter.
My code:
#include <iostream>
using namespace std;
int main()
{
int n,m,s;
cin >> n;
cin >> m;
s=n*m;
char Tab[s];
for(int i=0; i<n*m; i++)
{
Tab[i]= 'A' + rand()%25;
}
for(int i=1; i<n*m; i++)
{
Tab[i+2]= 'a' + rand()%25;
}
for(int i=0; i<n*m; i++)
{
cout << Tab[i] << " ";
}
return 0;
}
Code-
#include <iostream>
#include <time.h>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
srand(time(NULL));// without this rand() function might continuously give the same value
while(n--){
int stringLen = (rand() % m) +1; // getting random length
string s=""; // taking null string
for(int i=0; i<stringLen; i++){
if(rand() % 2 == 0 ){ // capital or small letter
s += 'A' + (rand() % 26);
}else{
s += 'a' + (rand() % 26);
}
}
cout<<s<<" ";
}
}
Related
//Question
/*There are N seats in a row. You are given a string S with length N; for each valid i, the i-th character of S is '0' if the i-th seat is empty or '1' if there is someone sitting in that seat.
Two people are friends if they are sitting next to each other. Two friends are always part of the same group of friends. Can you find the total number of groups?
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains a single string S.
Output
For each test case, print a single line containing one integer ― the number of groups.*/
// my code
#include <iostream>
#include <string>
using namespace std;
int main() {
int t;
cin>>t;
int n=1e6;
for(int i=0;i<t;i++){
string g1;
cin>>g1;
int group;
group = 0;
for(int j=0;j<g1.length();j++){
if(g1[j] == '1'){
for(int h=1;h<n;h++){
if(g1[j+h] == '1'){
h++;
}else{
break;
}
group++;
}
} else{
continue;
}
}
cout<<group<<endl;
}
return 0;}
Example Input
4
000
010
101
01011011011110
Example Output
0
1
2
4
//my output
0
0
0
9
Based on sample output, you suppose to count '1's between zeros, which is the number of groups you have. Here is your implementation with small correction to do that.
#include <iostream>
#include <string>
using namespace std;
int main() {
int t;
cin >> t;
// int n = 1e6; --> not used
for (int i = 0; i < t; i++) {
string g1;
cin >> g1;
int group;
group = 0;
for (size_t j = 0; j < g1.length(); j++) {
if (g1[j] == '1') {
group++;
//skip all '1' before the first '0'
while (g1[j] == '1' && j < g1.length())
j++;
}
else {
continue;
}
}
cout << group << endl;
}
return 0;
}
// my code
#include <iostream>
#include <string>
using namespace std;
int main() {
int t;
cin >> t;
// you don't need n variable
// it is appropriate to use the length of the string instead
// it also will remove one warning for initialization
int n = 1e6;
for (int i = 0; i < t; i++) {
string g1;
cin >> g1;
int group; // you can use int group = 0; avoiding the statement below
group = 0;
// size_t stringLength = g1.length();
for (int j = 0; j < g1.length(); j++) {
if (g1[j] == '1') {
group++; // you need to add it here to starts counting
// better this way -> for (size_t h = 1; h < stringLength; h++)
for (int h = 1; h < n; h++) { // h < n && (j+h)<stringLength
if (g1[j + h] == '1') {
// you increasing h value twice - first in for statement, second here
// instead, you need to set j to j+h value
//h++;
// you just "moving" through the string till next'0'
j = j + h;
}
else {
break;
}
// this will increase group count for each next '1' in the string
// this is why you got 9 for the last string in your's example
//group++;
}
}
// this is not needed
//else {
// continue;
//}
}
cout << group << endl;
}
return 0;
}
So recently i got a programing task from a contest and my code should display the N
even digit number from 0. In even digit numbers all numbers must be dividable by 2. When i input the number 12 it should show the number 44 on the output but it shous some kind of a mess. here's my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int kturaParzysta = 1;
int N;
cin >> N;
int tab[N];
for(int i = 0; i < N;i++){
string cyfraStr = to_string(N);
bool parzystocyfrowa = true;
for(int j = 0;j <= cyfraStr.length();j++){
if(int(cyfraStr[j]) % 2 != 0){
parzystocyfrowa = false;
}
}
if(parzystocyfrowa){
tab[kturaParzysta] = i;
kturaParzysta++;
}
}
cout << tab[N - 1];
return 0;
}
I need help completing a task for class, basically I need to read letters from a text file, count each letters occurrence and sort them by most occurrences. I'm having a problem remembering each letters original count after sort (since index changes)
The problem is that the result letters are appearing more then once so I lose some of the letters
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main ()
{
char letter, b[26] = {};
int number[26] = {0}, temp, i, j;
ofstream outfile ("test.txt");
srand (time(NULL));
for(int i=0;i<20;i++){
char c = rand() % 26 + 65 + rand() % 2 * 32;
outfile <<c<<endl;
}
outfile.close();
ifstream file( "test.txt");
while(file >> letter){
if (letter >= 'a' && letter <= 'z') number[letter - 'a']++;
if (letter >= 'A' && letter <= 'Z') number[letter - 'A']++;
}
for(i=0; i<26; i++)
{
for(j=i+1; j<26; j++)
{
if(number[j] > number[i])
{
temp = number[i];
b[i] = char(97+j);
number[i] = number[j];
number[j] = temp;
}
}
}
for(i=0;i<26;i++){
if(number[i] != 0)cout<<b[i]<<" "<<number[i]<<endl;
}
return 0;
}
l 3
m 3
m 2
q 2
w 2
j 1
l 1
m 1
o 1
q 1
t 1
v 1
w 1
To broaden what i was saying in comments about using a struct, consider this:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct CharInstance {
int count;
char letter;
};
int main()
{
char letter;
int i, j;
CharInstance temp;
CharInstance charInst[26] = { 0 };
// create test data randomly
{
ofstream outfile("test.txt");
srand(time(NULL));
for (int i = 0; i < 500; i++) {
char c = rand() % 26 + 65 + rand() % 2 * 32;
outfile << c << endl;
}
outfile.close();
}
// read in data and store in array
{
ifstream file("test.txt");
while (file >> letter) {
// force lowercase to uppercase so we count both lowercase and uppercase as the same
if (letter >= 'a' && letter <= 'z') letter -= 32;
// since all input is now uppercase, ignore anything that isn't in uppercase range
if (letter < 'A' || letter > 'Z') continue; // skip non-alpha chars
const int ind = letter - 'A';
++charInst[ind].count;
charInst[ind].letter = letter;
}
file.close();
}
// bubble sort
for (i = 0; i < 26; i++)
{
for (j = i + 1; j < 26; j++)
{
if (charInst[j].count > charInst[i].count)
{
temp = charInst[i];
charInst[i] = charInst[j];
charInst[j] = temp;
}
}
}
for (i = 0; i < 26; i++) {
cout << charInst[i].letter << ": " << charInst[i].count << " " << endl;
}
return 0;
}
If you don't mind C++11.
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
int main() {
std::ifstream in("sort.cpp");
// Map to store counts of each character.
// Note: this won't work with some international characters (i.e. Chinese).
std::map<char, int> histogram;
char ch = 0;
while (in.get(ch)) {
// TODO
// Filter out characters you don't want, as this would also pick whitespaces
// and newlines.
// Increment the count for |ch|.
// This would create new map element if the count was zero.
++histogram[ch];
}
// Move map contents into an array of pairs character->count.
std::vector<std::pair<char, int>> pairs(histogram.begin(), histogram.end());
// Sort the array. The third parameter is a function used as a custom comparator.
std::sort(pairs.begin(), pairs.end(),
[](const std::pair<char, int>& left, const std::pair<char, int>& right) {
// Use the count to compare |left| and |right|.
return left.second > right.second;
});
for (const auto& pair : pairs) {
std::cout << pair.first << " " << pair.second << std::endl;
}
}
g++ -std=c++11 sort.cpp
./a.out
207
t 79
a 62
e 61
r 61
i 53
s 51
o 50
n 50
c 46
...
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string sentence = "some random sentence";
int i = 0; //runs through the bigger string
int j = 0; //runs through the smaller string
int k = 0; //variable to mark the position where the string starts being equal in order to delete it using substring
string remove = "random";
int a = sentence.size();
int b = remove.size();
while (i < a)
{
if (sentence[i] == remove[j])
{
if (b == j - 1)
{
cout << sentence.substr(0, k) << sentence.substr(i, (a - 1));
break;
}
else
{
i++;
j++;
}
}
else
{
i++;
j = 0;
k++;
}
}
return 1;
}
I want to remove the word random from the bigger string and print it out but when I run the code, it does not return anything. What's missing?
I already tried putting a break right below de "cout", but it does not work.
Thank you :)
As b == 6, j would have to be 7 in order for b == j-1 to become true. But remove[6] is the terminating \0 of the random string, so j can never grow beyond 6.
Here is the code I edited
if (b-1 == j)
{
cout << sentence.substr(0, k) << sentence.substr(i+2, (a - 1));
break;
}
This is assuming, you have spaces between the words.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int main ()
{
int a [99];
srand (time(NULL));
for(int i = 0; i <=9; i++)
{
a[i] = rand() % 10 + 1; // Random number range from 1 to 10.
cout << "index # "<< i <<"\t"<<a[i] << " "<< endl;
if(i != 0) /*to prevent a[0] and a[0] from comparing each other*/
{
int n = i - 1; //counter
while( n!= -1) // to control the loop.
{
if(a[i] == a[n])
{
a[i] = rand() % 10 + 1; //reassign a random number to that index location.
n = i; //reset counter if there is a match number.
}
n--; //counts from the highest index No. to the lowest.
}
}
}
return 0;
}
ps:
To generate random numbers from 1 - 10.
It kept giving me duplicated numbers,
How can I make it not to duplicate the same numbers,
The while loop I used doesn't seem to work at all
Target Example Output would be like:
1
5
6
3
2
4
8
9
7
10
Instead of bunch of duplicated single digit numbers.
You have to print the result of the process to prevent duplicate numbers from printed.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int main ()
{
int a [99];
srand (time(NULL));
for(int i = 0; i <=9; i++)
{
a[i] = rand() % 10 + 1; // Random number range from 1 to 10.
// remove this
//cout << "index # "<< i <<"\t"<<a[i] << " "<< endl;
if(i != 0) /*to prevent a[0] and a[0] from comparing each other*/
{
int n = i - 1; //counter
while( n!= -1) // to control the loop.
{
if(a[i] == a[n])
{
a[i] = rand() % 10 + 1; //reassign a random number to that index location.
n = i; //reset counter if there is a match number.
}
n--; //counts from the highest index No. to the lowest.
}
}
// add this
cout << a[i] << endl;
}
return 0;
}