Question

In: Computer Science

How do I use ASCII code to generate and display two random letters that are one...

How do I use ASCII code to generate and display two random letters that are one lowercase and one uppercase in C++?

Please give me an example.  

Solutions

Expert Solution

//To generate letters from ASCII value. First, we need to get the values for the alphabets Uppercase and Lowercase

//Uppercase 'A' has ASCII value 65........ 'Z' has the value 90.

//Lowercase 'a' has value 97........ 'z' has the value 122.

//so in order to generate random uppercase and lowercase characters we need to generate random values of the above range and then typecasting it to char type while in the cout statement.

NOTE: the values are compiler dependent and rand() function may cause error then include stdlib.h

--------------------------------------------------------------------------code-------------------------------------------------------------------------------------

#include <iostream>

using namespace std;

int main()
{
   int u_max=90,u_min=65; //declaring upper and lower range for uppercase letter
   int l_max=122,l_min=97; //declaring range for lower case letter
  

//calculating a random int value in the range of upper case letters


   int random_upper = (rand() %(u_max + 1 - u_min)) + u_min;

//calculating a random int value for lower case letter


   int random_lower = (rand() %(l_max + 1 - l_min)) + l_min;

//printing both the letters by typecasting inti char type


   cout<<"Random uppercase letter for ASCII value "<<random_upper<<" : "<<(char)random_upper<<endl;
   cout<<"Random lowercase letter for ASCII value "<<random_lower<<" : "<<(char)random_lower<<endl;
   return 0;
}


Related Solutions

Show the code in matlab to display an ECG graph (I do not want code that...
Show the code in matlab to display an ECG graph (I do not want code that simply calls the ecg function in matlab but how to write that kind of code)
C++ program Generate and display two random characters involving upper and lowercase
C++ program Generate and display two random characters involving upper and lowercase
Write the R code to generate five independent uniform random numbers and use that to generate...
Write the R code to generate five independent uniform random numbers and use that to generate 5 independent Bernoulli random variables with probability of success p = 0.4. please use RStudio. please do not use lab nor rbern for calculations.
a code is 4 random letters and 3 random digits. what is the probability of the...
a code is 4 random letters and 3 random digits. what is the probability of the 4 letters all being different while the 3. numbers at the end are in ascending order? b) in a group of 10 codes.... what is the probability that at least two have all different letters and numbers in ascending order c) in 1000 codes what is the wxpected value and standard deviation for the codes with all different letters and ascending digits
How do I use ASCPII that produces two alphabets where one is uppercase and the other...
How do I use ASCPII that produces two alphabets where one is uppercase and the other lowercase using C++ Lets use Y and/or A  
How do i send random numbers in my Amazon SES email? What is the code for...
How do i send random numbers in my Amazon SES email? What is the code for it and where would it go Lambda or SES? Please provide step by step instructions
PHP Language I have XAMPP and I don't know how to display my PHP code. For...
PHP Language I have XAMPP and I don't know how to display my PHP code. For instance, when I create a basic HTML webpage I just open the code into a web browser and it shows me a basic web page. When I try to show a PHP calculation it doesn't show. What are the steps in displaying my PHP doc?
How do letters of credit and drafts work together? How does this create one of the...
How do letters of credit and drafts work together? How does this create one of the most liquid money market instrument?
Write VHDL code for the following: Use HEX-to-seven segment display converters to display the inputs and...
Write VHDL code for the following: Use HEX-to-seven segment display converters to display the inputs and results for a 4-bit adder. The inputs are unsigned 4-bit binary numbers. The outcome is a 4-bit binary adder with LED display. First you need to create a symbol for the HEX-to-seven segment display converter. Then implement a 4-bit adder using VHDL. Finally, connect three HEX-to-seven segment display converters to display input X, input Y, and sum S.
How do I generate a correlation matrix in SPSS, what are the steps?
How do I generate a correlation matrix in SPSS, what are the steps?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT