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

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 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
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  
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 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?
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?
How to display in SQL Server, for example, all the author First Names are FIVE LETTERS...
How to display in SQL Server, for example, all the author First Names are FIVE LETTERS LONG if I have the Author Table. I used the WHERE LENGTH ua_fname=5   This is not returning names with 5 letters. Can someone please suggest to me the correct one?
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
What is the code in Rstudio or R? (a) Generate 200 random samples of size n...
What is the code in Rstudio or R? (a) Generate 200 random samples of size n = 10 from a Poisson distribution with mean λ = 12. i. Calculate sample means for each sample. Report the first 10 sample means. ii. Draw a histogram of the sample means (where the y-axis is the density) and fit a density estimate (default density estimator is ok). iii. What is your finding about the sampling distribution of the sample mean, based on your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT