Concept explainers
Given the declaration of a C-string variable, where SIZE is a defined constant:
char ourString[SIZE];
The C-string variable ourString has been assigned in code not shown here. For correct C-string variables, the following loop reassigns all positions of ourString the value ‘X’, leaving the length the same as before. Assume this code fragment is embedded in an otherwise complete and correct
int Index = 0;
while (ourString[index] != ‘\0’)
{
outString[index] = ‘X’;
index++;
}
a. Explain how this code can destroy the contents of memory beyond the end of the array.
b. Modify this loop to protect against inadvertently changing memory beyond the end of the array
Want to see the full answer?
Check out a sample textbook solutionChapter 8 Solutions
Problem Solving With C++ (Looseleaf) - With Access
Additional Engineering Textbook Solutions
Computer Science: An Overview (12th Edition)
C++ How to Program (10th Edition)
Artificial Intelligence: A Modern Approach
Absolute Java (6th Edition)
Starting Out With Visual Basic (8th Edition)
Software Engineering (10th Edition)
- In this problem, you are required to use for/while loop only to implement a calculator using++ (increment) and -- (decrement) operators only. The operations that your calculator shallperform are limited, i.e. addition, subtraction, multiplication, division, remainder andabsolute. Your program shall ask the user about the two input values and an operator, andthen use the switch-case structure to run the required operation (addition, subtraction,multiplication, division or absolute (symbol ~)).Prototype are:• int addition(int n1,int n2);• int subtraction(int n1,int n2);• int multiplication(int n1,int n2);• int division(int num,int denom);• int remainder(int num,int denom);• int absolute(int num); op n1 n2 + 10 12 22 1+ 10 -12 -2 2+ -10 12 2 2+ -10 -12 -22 1- 10 12 -2 1- 10 -12 22 1- -10 12 -22 2- -10 -12 2 2* 10 12 120 1* 10 -12 -120 2* -10 12 -120 2 * -10 -12 120 1/ 20 3 6 1/ 20 -3 -6 1/ -20 3 -6 1/ -20 -3 6 1/ 3 5 0 1/ 3 0 error 1% 20 3 2 3% 3 5 3 1~ 5 5 1~ -5 5 3 Note only use…arrow_forwardIn this assignment, you are asked to write C program to assist mathematicians on Collatz conjecture, which receives a positive input n from the command line when user runs the program, the details of the algorithm could be found from the wiki page above. Essentially, for an odd number, the algorithm will multiply the number by 3 and add 1, for an even number, divide it by 2, to produce a new number, repeat the above procedures as needed, the sequences of integers will be built until it arrives at 1. e.g., if you provide 16, then the sequence is 16, 8, 4, 2, 1; if you provide 15, the sequence is 15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1... and so on. Task list: 1. implement a sequential version of the Collatz conjecture implementation, and measure the time needed for the calculation 2. You are asked to use fork() to made or create a child process, and use the child process to do the job and output the sequence, measure the time needed to create the child…arrow_forwardIn python, The function remove_vowels takes one parameter, s, a string of any length. The function then returns a new string that contains all the letters in s except for the vowels: 'a', 'e', 'i', 'o', 'u'. You may assume the string contains no upper-case letters. Hint: Accumulate the new string. - Use a for loop to iterate over the characters in s - Inside the loop, update the accumulator if the current character is not a lowercase vowel ( if ch not in 'aeiou' ) For example: Test Result print(remove_vowels("giraffe")) grff print(remove_vowels("love bug")) lv bg print(remove_vowels("racecar")) rccr print(remove_vowels("uoiea"))arrow_forward
- Please help me with my assignment Consider the loop from Section 8.3 of your textbook prefixes = JKLMNOPQ suffix = "ack" for letter in prefixes: print (letter + suffix) Put this code into a Python script and run it. Notice that it prints the names in "Oack and Qack". Modify the program so that it prints "Ouack and "Quack" but leaves the other names the same include the modified Python code and the output in your submission. 2. Give at least three examples that show different features of string slices. Describe the feature illustrated by each example. Invent your own examples. Do not copy them for the textbook or any other source.arrow_forwardThe following requirments that weren't mentioned for solving the following Python Code below: The provided code for alphabet, test_dups, test_miss, and histogram. Your implementation of the has_duplicates function. A loop that outputs duplicate information for each string in test_dups. Your implementation of the missing_letters function. A loop that outputs missing letters for each string in test_miss. Write a function called missing_letters that takes a string parameter and returns a new string with all the letters of the alphabet that are not in the argument string. The letters in the returned string should be in alphabetical order. Your implementation should use a histogram from the histogram function. It should also use the global variable alphabet. It should use this global variable directly, not through an argument or a local copy. It should loop over the letters in alphabet to determine which are missing from the input parameter. The function missing_letters should…arrow_forwardIn this problem, you are required to use for/while loop only to implement a calculator using++ (increment) and -- (decrement) operators only. The operations that your calculator shall perform are limited, i.e. addition, subtraction, multiplication, division, remainder and absolute. Your program shall ask the user about the two input values and an operator, and then use the switch-case structure to run the required operation (addition, subtraction, multiplication, division or absolute (symbol ~)).Prototype are: int addition(int n1,int n2); int subtraction(int n1,int n2); int multiplication(int n1,int n2); int division(int num,int denom); int remainder(int num,int denom); int absolute(int num);arrow_forward
- In this lab, you add a loop and the statements that make up the loop body to a C++ program that is provided. When completed, the program should calculate two totals: the number of left-handed people and the number of right-handed people in your class. Your loop should execute until the user enters the character X instead of L for left-handed or R for right-handed. The inputs for this program are as follows: R, R, R, L, L, L, R, L, R, R, L, X Variables have been declared for you, and the input and output statements have been written. Instructions Ensure the source code file named LeftOrRight.cpp is open in the code editor. Write a loop and a loop body that allows you to calculate a total of left-handed and right-handed people in your class. Execute the program by clicking the Run button and using the data listed above and verify that the output is correct. **GIVEN CODE** #include<iostream> using namespace std; int main(){ string leftOrRight = ""; // L or R for one…arrow_forwardIn this lab, you add a loop and the statements that make up the loop body to a C++ program that is provided. When completed, the program should calculate two totals: the number of left-handed people and the number of right-handed people in your class. Your loop should execute until the user enters the character X instead of L for left-handed or R for right-handed. The inputs for this program are as follows: R, R, R, L, L, L, R, L, R, R, L, X Variables have been declared for you, and the input and output statements have been written. Instructions Ensure the source code file named LeftOrRight.cpp is open in the code editor. Write a loop and a loop body that allows you to calculate a total of left-handed and right-handed people in your class. Execute the program by clicking the Run button and using the data listed above and verify that the output is correct. **CODE GIVEN** // LeftOrRight.cpp - This program calculates the total number of left-handed and right-handed //…arrow_forwardHi, Can you change(write) this code a little differrent? and this is the question but pls change this code Write a computer program for Gauss elimination method using C programming language. Decide the number of significant figures yourselves. While writing your program, consider the effects of the number of significant figures, pivoting, scaling and do not forget to check if the system is ill conditioned. #include<iostream>#include<cmath>using namespace std;int main(){int n,i,j,k;// initializing variablescout<<"\nEnter the no. of equations\n";cin>>n; //input the no. of equationsfloat matrix[n][n+1],x[n]; //declare an array to store the elements of augmented-matrixcout<<"\nEnter the elements of the augmented-matrix row-wise:\n";for (i=0;i<n;i++)// taking the input from the userfor (j=0;j<=n;j++)cin>>matrix[i][j];//input the elements of arrayfor (i=0;i<n;i++) //Pivotisation for the given matrixfor (k=i+1;k<n;k++)if…arrow_forward
- My assignment want me to come up with a code in C++ that would remove any non-alphebet character from a input string using loop. I have not getting the full answer I was expecting and seems like my loop is cutting short. I have attached a screenshot of the code I come up with. example input: -Hello, 1 world$! expected output: Helloworldarrow_forwardPython 3.7.4: Write a loop that reads strings from standard input, where the string is either "duck" or "goose". The loop terminates when "goose" is read in. After the loop, your code should print out the number of "duck" strings that were read. ----------------- This is what I tried so far, and it's been marked wrong by MyProgrammingLab: count = 0inputstring=input("Enter goose/duck: ")countducks=0while inputstring!="duck": countducks+=1 inputstring=input("Enter either the word goose or duck: ")print(countducks, "This is the number of times duck was entered:" count)arrow_forwardWrite a complete C program that inputs marks of a student in 5 courses using any loop and writes these marks into an output file named txt. After that, it reads scores from myElements.txt and computes the grades based on the table below using a programmer-defined function char computeGrade(double score), and displays the grades on screen. Score Grade >= 90 A 80-89 B 70-79 C 60-69 D <= 59 F ----- please, write 1. the c program code 2. include comments on your code 3. contents of the file myElements.txt 4. sample output Thank you so mucharrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education