(Write C++ Statements) Write a statement for each of the following:
- Print integer 40000 left justified in a 15-digit field.
- Read a string into character array variable state.
- Print 200 with and without a sign.
- Print the decimal value 100 in hexadecimal form preceded by 0x.
- Read characters into array charArray until the character ‘p’ is encountered, up to a limit of 10 characters (including the terminating null character). Extract the delimiter from the input stream, and discard it.
- Print 1.234 in a 9-digit field with preceding zeros.
a.
To print integer 4000 left justified in a 15-digit field.
Explanation of Solution
The function setw() is used in C++ to adjust the field width for output.
intleftalign=4000;
cout<<setw(15)<<leftalign;
b.
To read a string into character array variable state.
Explanation of Solution
Following statement will read as string into character array variable state.
cin>>state;
c.
To print 200 with and without a sign.
Explanation of Solution
Following statement will print the number 200 with sign
cout<< “+200”;
Following statement will print the number 200 without sign
cout<< “200”;
d.
To print the decimal value 100 in hexadecimal form preceded by 0x.
Explanation of Solution
Following set of statement will print the decimal value 100 as specified.
cout<<”Print the value decimal value 100 in hexadecimal format”;
inttoHex = 100;
cout<<”The decimal number 100 converted to hexadecimal =”;
e.
To read the characters into array charArray until the character ‘p’ is encountered, upto a limit of 10 characters (including the terminating null character). To extract the delimiter from the input stream and discard it.
Explanation of Solution
char array[10]; for( int i=0;i<10; i++) { if(array[i]!=‘p’) { cout<<array[i]; } else break; } array[--i] = “”;
f.
To print 1.234 in a 9-digit field with preceding zeros.
Explanation of Solution
Following statement will print 1.234 with 9 digit field with preceding zeros.
cout<<setfill('0')<<setw(9)<<1.234;
Want to see more full solutions like this?
Chapter 21 Solutions
C How to Program (8th Edition)
Additional Engineering Textbook Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Database Concepts (8th Edition)
Java How To Program (Early Objects)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
- Correct my mistake in my C++ code please! Here is the question and my code is below as well. (Duplicate Elimination with array) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it isn't a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the 'worst case' in which all 20 numbers are different. Use the smallest possible array to solve this problem. Code; #include <iostream> using namespace std; int main(){ int arr[100]={0};int temp,i;for( i=0;i<20;i++){ cout<<"Enter an Integer : "; cin>>temp; arr[temp]++;}for( i=0;i<100;i++) if(arr[i]==1) cout<<i<<" "; return 0;}arrow_forwardI want this in c++ programming language sir.arrow_forward(Locate the largest element) Write the following function that finds the location of the largest element in a two-dimensional array. void 1ocateLargest(const double a[][4 ], int 1ocation[]) The location is stored in a one-dimensional array location that contains two elements. These two elements indicate the row and column indices of the largest element in the two-dimensional array. Write a test program that prompts the user to enter a 3 × 4 two-dimensional array and displays the location of the largest element in the array.arrow_forward
- Complete/Modify the code satisfying the following: (put comments pls) in C++, pls put comments in the code 1. Create a 10x10 matrix using rand function to fill the said matrix with random numbers 2. Print all the numbers in matrix form 3. Ask the user the following options here are the options A. Sum per row B. Sum per column C. Display all ODD numbers horizontally D. Display all EVEN numbers horizontally E. Display all PRIME numbers horizontally F. Generate new set of random numbers G. Close the program After asking what option to display, Display again the previous numbers entered and the same options. Here's the code: #include<stdio.h>#include<math.h> main(){ char rep; rep = 'y'; while(rep=='y'||rep =='Y') { int cntr01=1,xsum=0,numrow,numcol; char option; int x[3][3]; //creates --> x[0][0],x[0][1],x[0][2] //creates --> x[1][0],x[1][1],x[1][2] //creates --> x[2][0],x[2][1],x[2][2] //cntr01++ <-->…arrow_forward. (True/False): If you pass a variable containing the offset of an array of bytes to a procedurethat expects a pointer to an array of words, the assembler will flag this as an error.arrow_forward[hey expert! kindly answer is simple c++ ] :) You are required to write a program that takes input from the user the marks of his programming fundamentals quizzes in an array. You are required to write the program that caters for 10 students. That means the program asks the user to enter 10 quizzes marks for 10 students (use 2 dimensional array). The program calculates the percentage of all 10 quizzes for each student.Based on the percentage, the program should calculate the grade of each student and store it in a char-one dimensional array. The program should also display the grade of the student on the screen. The grade is calculated by the formula percentage>=80 --- grade = A for percentage >=70 and less than 80 ---------- grade = B for percentage >=60 and less than 70--------- grade = C for percentage >=50 and less than 60 ----------grade = D for percentage less than 50 ----------grade= F In the end when all the grades have been calculated the program should…arrow_forward
- Indicate true or false for the following statements: a. Every element in an array has the same type. b. The array size is fixed after an array reference variable is declared. c. The array size is fixed after it is created. d. The elements in an array must be of a primitive data typearrow_forward- array from user and print all the elements after doubuq have to separate function for doubling operation. 2arrow_forwardQ2: Write a C++ function (call it index_max) that finds the maximum number in a 1-D array of floats, that is passed to the function, and returns the max number and its index. For example, if you pass [5.1, 6, 8.5, 9, 5.6, 2.7] to the function, you obtain 9 and 3. Write a C++ program and test your function. Take the following note into consideration when writing your program: • The function should prevent changing the elements of the passed array.arrow_forward
- 3. Write the code to print the following diamond. Symbol used to draw the diamond and number of rows must be taken as input from the user. Symbols should be in a cell array, and selected using the index taken from the user. Symbol list should include *, +', 'o', respectively. Example output for the program: Enter number of rows: Enter index for the symbol for diamond: 2 +++ +++++ +++++++ +++++++++ +++++++ +++++ +++ +arrow_forwardRequired part 4,5,6,7arrow_forwardComplete/Modify the code satisfying the following: (put comments pls) in C++, pls put COMMENTS in the code 1. Create a 10x10 matrix using rand function to fill the said matrix with random numbers 2. Print all the numbers in matrix form 3. Ask the user the following options OPTIONS: A. Total sum, average, median per row B. Total sum, average, median per column C. Display the same matrix but all ODD numbers are replaced with "-" (minus sign) D. Display the same matrix but all EVEn numbers are replaced with "+" (plus sign E. Display the same matrix but all PRIME numbers are replaced with "*" (asterisksign) F. Display the whole matrix with every row in ascending order G. Display the whole matrix with every row in descending order H. Display the whole matrix with every column in ascending order I. Display the whole matrix with every cloumn in descending order J. Generate new set of random numbers Q. Close the program After asking what option to display, Display again the previous numbers…arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr