//This code gives randomly generated alphabets and if equal will
//cout the alphabet which is equal
1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 using namespace std;
5 int main()
6 {
7 int a;
8 char array[10];
9 srand (time(0));
10 for (int i=0; i<10; i++)
11 {
12 a=rand() %26;
13 a=a+97;
14 array[i]=char(a);
15 }
16 for (int i=0; i<10; i++)
17 {
18 for (int j=0; j<10; j++)
19 {
20 if (array [i]==array [j]);
21 {
22 cout <<array[i] <<" " <<array[j];//if alphabet 'c' is at indecis 2,5,8
//then output should be like that of
// only 22 no statement but actually it does
// not give this answer but gives wrong output
//c c
//c c
//c c
23 }
24 cout << endl;
25 }
26 }
27 return 0;
28 } //program end
My question is how to check that randomly generated alphabets are equal e.g in 22 number line it should give output of equal alphabets if they are equal but it does not give equal alphabets what wrong in this code mention the wrong statement or help me how will i get right answer
Actually i want to make a program that should tell me how many times a single generated alphabet comes in an array
According to your comment you're looking for something that does this:
#include <random>
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
int main(){
::std::vector<char> letters(10);
::std::vector<char> unique;
::std::random_device rd;
::std::mt19937 mt(rd());
::std::uniform_int_distribution<char> dis('a', 'z');
auto random = ::std::bind(dis, mt);
// fill the vector with random letters
for(char & l : letters){
l = random();
}
int max_count = 0;
char appeared_most = ' ';
// get a vector of all the unique values
::std::unique_copy(letters.cbegin(), letters.cend(), ::std::back_inserter(unique));
// find the first value to appear most if two values appear equally
// the first will be chosen
for(const char & l : unique){
int count = ::std::count(letters.cbegin(), letters.cend(), l);
if(count > max_count){
max_count = count;
appeared_most = l;
}
}
::std::cout << appeared_most << " " << max_count ;
}
I have a working sample here: http://coliru.stacked-crooked.com/a/53c953576e0db756
Related
I have a c code to show a lowercase of one argument:
This code runs ok when called from commandline with more than 6 argument
however,if called without argument, the demo part does not work ,and programme got stuck:
Below is the code file:
Can anyone help me, Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
int showLowerCase(char *argv[]){
char* aStr = argv[6];
//To lowercase: the demo got stuck probably here
for (int i = 0; aStr[i]; i++) {
aStr[i] = tolower(aStr[i]);
}
printf("a lower case for 6th input string: %s\n",aStr);
}
int main(int argc, char *argv[])
{
if(argc < 7){
// when no srguments given, try a demo:
// change argc to 7
argc = 7;
// make a example of argv2 with 7 strings
char *argv2[7];
argv2[0] = "killWindowsVersatile.exe";
argv2[1] = "key";
argv2[2] = "ci";
argv2[3] = "once";
argv2[4] = "2";
argv2[5] = "1000";
argv2[6] = "SuperCol"; //this argument will be shown as lowercase
showLowerCase(argv2);
return 0;
}
showLowerCase(argv);
return 0;
}
It seems that argv can be modified to lowercase in site, while the argv2 I constructed cannot be modified.
Solved by strcpy:
int showLowerCase(char *argv[]){
printf("aStr = argv[6]\n");
char* aStr = argv[6];
printf("aStr = argv[6]...done");
//make hard copy
char newStr[strlen(aStr)+1];
strcpy(newStr,aStr);
//To lowercase
for (int i = 0; newStr[i]; i++) {
printf("i=0\n");
printf("%c\n",newStr[i]);
newStr[i] = tolower(aStr[i]);
}
printf("a lower case for 6th input string: %s\n",newStr);
}
I want read a paragraph by extracting one word at a time using Multithreading . Each thread should read exactly one word and when paragraph ends they should exit peacefully . I know threads shouldn't be used in this way as there is no advantage in that . But I want to do that so that I can check how threads can work sequentially if required . I tried but it looks like the program is reaching deadlock state and not giving any output at all . There are 11 words in the string and I am using 4 threads .
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>
#include <chrono>
#include <condition_variable>
using namespace std;
stringstream s("Japan US Canada UK France Germany China Russia Korea India Nepal");
int count = 0;
string word;
condition_variable cv;
mutex m;
int i = 0;
bool check_func(int i,int k)
{
return i == k;
}
void print(int k)
{
while(count < 11) // As there are 11 words
{
unique_lock<mutex> lk(m);
int z = k;
cv.wait(lk,[&]{return check_func(i,z);}); // Line 33
s >> word;
cout<<word<<" ";
i++;
cv.notify_all();
count++;
}
return;
}
int main()
{
thread threads[4];
for(int i = 0; i < 4; i++)
threads[i] = thread(print,i);
for(auto &t : threads)
t.join();
return 0;
}
You need to change this code:
while(count < 11) // As there are 11 words
{
unique_lock<mutex> lk(m);
int z = k;
cv.wait(lk,[&]{return check_func(i,z);}); // Line 33
s >> word;
cout<<word<<" ";
i++;
cv.notify_all();
count++;
}
To this:
int z = k;
while(z < 11) // As there are 11 words
{
unique_lock<mutex> lk(m);
int z = k;
cv.wait(lk,[&]{return check_func(i,z);}); // Line 33
s >> word;
cout<<word<<" ";
i++;
z+=4;
cv.notify_all();
count++;
}
Three things changed in the above: the declaration of z is moved outside of the loop, the condition in the while changed to check z instead of count, and the line z+=4 added. You need to increment z by 4 each time if you want each of your threads to move onto the next word. Also, you need to check z not count because otherwise some of the threads will overshoot and the last word will be printed multiple times (consider the thread that reads the ninth word: if checking on count instead of z it continues on to the next iteration of the loop even though there will be nothing further for it to read).
I am working on the Vigenere exercise from Harvard's CS50 (in case you noticed I'm using string and not str).
My program gives me a Floating Point Exception error when I use "a" in the keyword.
It actually gives me that error
when I use "a" by itself, and
when I use "a" within a bigger word it just gives me wrong output.
For any other kind of keyword, the program works perfectly fine.
I've run a million tests. Why is it doing this? I can't see where I'm dividing or % by 0. The length of the keyword is always at least 1. It is probably going to be some super simple mistake, but I've been at this for about 10 hours and I can barely remember my name.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main (int argc, string argv[])
{
//Error message if argc is not 2 and argv[1] is not alphabetical
if (argc != 2)
{
printf("Insert './vigenere' followed by an all alphabetical key\n");
return 1;
}
else if (argv[1])
{
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isalpha((argv[1])[i]) == false)
{
printf("Insert './vigenere' followed by an all alphabetical key\n");
return 1;
}
}
//Store keyword in variable
string keyword = argv[1];
//Convert all capital chars in keyword to lowercase values, then converts them to alphabetical corresponding number
for (int i = 0, n = strlen(keyword); i < n; i++)
{
if (isupper(keyword[i])) {
keyword[i] += 32;
}
keyword[i] -= 97;
}
//Ask for users message
string message = GetString();
int counter = 0;
int keywordLength = strlen(keyword);
//Iterate through each of the message's chars
for (int i = 0, n = strlen(message); i < n; i++)
{
//Check if ith char is a letter
if (isalpha(message[i])) {
int index = counter % keywordLength;
if (isupper(message[i])) {
char letter = (((message[i] - 65) + (keyword[index])) % 26) + 65;
printf("%c", letter);
counter++;
} else if (islower(message[i])) {
char letter = (((message[i] - 97) + (keyword[index])) % 26) + 97;
printf("%c", letter);
counter++;
}
} else {
//Prints non alphabetic characters
printf("%c", message[i]);
}
}
printf("\n");
return 0;
}
}
This behavior is caused by the line keyword[i] -= 97;, there you make every 'a' in the key stream a zero. Later you use strlen() on the transformed key. So when the key starts with an 'a', keywordLength therefor is set to zero, and the modulo keywordLength operation get into a division by zero. You can fix this by calculating the keyword length before the key transformation.
I have created a Fibonacci program that runs correctly. However I can not figure out how to format the output window the way the problem would like. The rows and spacing are correct but the program should display 6 columns, as it is now the program outputs nine with the ninth cut off. Am I doing something wrong or missing something? I am using the Visual Studio C++ compiler.
#include <iostream>
#include <iomanip>
using namespace std;
void main ()
{
int FirstNum = 1;
int SecondNum = 0;
int Count = 1;
int Answer;
do
{
Answer = FirstNum + SecondNum;
FirstNum = SecondNum;
SecondNum = Answer;
cout << FirstNum << setw (10);
Count++;
} while (Count < 40);
}
This code will generated only 6 columns.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int FirstNum = 1;
int SecondNum = 0;
int Count = 1;
int Answer;
do
{
Answer = FirstNum + SecondNum;
FirstNum = SecondNum;
SecondNum = Answer;
cout << setw (10)<< FirstNum ;
Count++;
if(Count%6==0)
cout<<endl;
} while (Count < 40);
return 0;
}
i want to convert decimal values to 16 bit binary values. i used this code in another one.
#include <iostream>
#include <bitset>
int main() {
int x = 5;
std::bitset<8> bin_x(x);
std::cout << bin_x;
return 0;
}
this is a code posted by a member. i want to use it in a loop and store the value of bin_x in a 16 two dimensional character array. how can it be done?
here is what iam doing
#include<iostream>
using namespace std;
#include <bitset>
int main(){
int DecimalArray[] = {1,2,3,4,5,22,555,85,18,741}; //Create an array of decimal numbers.
const int ArrayLen = sizeof(DecimalArray)/sizeof(int); //Store the size of the Decimal Array in a constant
//strcpy(BinaryArray[i], "0000000000000000");
char BinaryArray[ArrayLen][16]; //Create an array of the same length for binary nos.
for(int i = 0; i<ArrayLen; i++)
{
int CurrentDec = DecimalArray[i]; //Store current Decimal number in CurrentDec variable
strcpy(BinaryArray[i], "0000000000000000");
std::bitset<16> bin_x(CurrentDec);
cout<< "bin"<<bin_x<< endl;
for (int j = 0; j<15; j++)
{
bin_x=BinaryArray[i][j];
cout<< "b1"<< BinaryArray[i] << endl;
}
cout<<"The Decimal numbers and their Binary Equivalents are:\n\n";
cout<<"Decimal Binary \n\n";
}
//Output both arrays
for( i = 0; i<ArrayLen; i++){
cout<<DecimalArray[i]<<"\t "<<BinaryArray[i]<<endl;
}
cin.get();
return 0;
}
but i do not get the value in BinaryArray. kindly help me with it, its very urgent. Thanks!
#include<iostream>
using namespace std;
#include <bitset>
int main(){
int DecimalArray[] = {1,2,3,4,5,22,555,85,18,741}; //Create an array of decimal numbers.
const int ArrayLen = sizeof(DecimalArray)/sizeof(int); //Store the size of the Decimal Array in a constant
//strcpy(BinaryArray[i], "0000000000000000");
char BinaryArray[ArrayLen][17]; //Create an array of the same length for binary nos.
int i;
for(i = 0; i<ArrayLen; i++)
{
int CurrentDec = DecimalArray[i]; //Store current Decimal number in CurrentDec variable
int index = 1, CurrentBin = 0;
strcpy(BinaryArray[i], "0000000000000000");
std::bitset<16> bin_x(CurrentDec);
cout<< "bin"<<bin_x<< endl;
for (int j = 0; j<16; j++)
{
if (bin_x[15-j])
{
BinaryArray[i][j] = '1';
}
cout<< "b1"<< BinaryArray[i][j]<<endl ;
}
}
cout<<"The Decimal numbers and their Binary Equivalents are:\n\n";
cout<<"Decimal Binary \n\n";
//Output both arrays
for( i = 0; i<ArrayLen; i++){
cout<<DecimalArray[i]<<"\t "<<BinaryArray[i]<<endl;
}
cin.get();
return 0;
}