15.6: Fix the code to print the count from 1 to x (to loop x times)
#include <iostream> // include the header file
using namespace std;
void count(int x){
for(int i=1; i<=x; i++){
cout<<x;
}
}
int main(){
int x,i;
cin >> x;
count(x);
return 0;
}
15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x"
#include <iostream> // include the header file
using namespace std;
int main(){
int x;
cin >>x;
for(int i=1; i<=x; i++)
for(int j=1; j<=x; j++)
cout<<x;
return 0;
}
16.4 Fix the code to print the last element of the array
#include <iostream>
using namespace std;
int main(){
int array[7]; // an array of size 7
int x=0;
cin >> x;
for(int i = 0 ; i < 7; i++){ // array initialization
array[i] = x;
}
20.6 make both pointers point at the same variable then use use one of the pointers to change it
#include <iostream>
using namespace std;
//make both pointers point at the same variable
//then use use one of the pointers to change it
int main(){
int i;
cin >> i;
cout << i << endl;
int * p;
int * q;
//get user input for i using one of the pointers
cout << *q << endl;
cout << *p << endl;
cout << (p == q) << endl;
}
cout << "The last element is: "<<x;
return 0;
}
#include <iostream> // include the header file using namespace std; void count(int x){ for(int i=1; i<=x; i++){ cout<<i<<" "; } } int main(){ int x,i; cin >> x; count(x); return 0; }
========================================
#include <iostream> // include the header file using namespace std; int main(){ int x; cin >>x; for(int i=1; i<=x; i++) cout<<x; for(int j=1; j<=x; j++) cout<<x; return 0; }
========================================
#include <iostream> using namespace std; int main(){ int array[7]; // an array of size 7 int x=0; cin >> x; for(int i = 0 ; i < 7; i++){ // array initialization array[i] = x; } cout<<array[6]; return 0; }
========================================
#include <iostream> using namespace std; //make both pointers point at the same variable //then use use one of the pointers to change it int main(){ int i; cin >> i; cout << i << endl; int * p; int * q; p = q; cout << *q << endl; cout << *p << endl; cout << (p == q) << endl; } cout << "The last element is: "<<x; return 0; }
15.6: Fix the code to print the count from 1 to x (to loop x times)...
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){ int rows = 5; int cols = 5; int x; int** arr = new int[rows][cols] cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x]; return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){ int rows; int cols; int x;...
Fix the code to print the "exact" result of the division of a/b (Pass by reference) #include <iostream> using namespace std; void division (int& a, int& b, int& result) { result = a/b; } int main () { float a, b, result; cin >> a; cin >> b; division(a, b, result); cout << "result = " << result << endl; return 0; }
Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){ int y; int result; cin >> y; result = positiveOrNegative(y); return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
Fix the code to print the result[] array properly: 1 #include <iostream> 2 3 using namespace std; 4 5 void printArray(int array 7 for(inti= 0: i < 2;i++) cout くく array[i] << " ". 10 12 int mainO 13 14 int firstArray[2]10,20}; 16 int resultr21 17 int x; 18 19 II take input for second array 20 cin >>x 21 secondArray[0]-x; 22 secondarrayL1」-X+15 ; // Add elements 23 24 result0]- firstArray[0] + secondArray [0]; 25 result [1]- firstArray[1] + secondArray[1];...
read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() { int Size; int count; cout << "enter the size: " << endl; cin >> Size; int...
I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() { int var1 = 20 ; int var2 = 30 ; int* ptr1 ; int* ptr2 ; int* temp ; ptr1 = &var1 ; ptr2 = &var2 ; cout << *ptr1 << endl ;...
12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print ciphertext character correctly. With offset of 3 the output should be ORIGINAL CHARACTERS A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ENCRYPTED CHARACTERS D E F G H I J K L M N O P Q R S T U V W X Y Z...
Make the pointer point at the 5th element of an array 1 #include <iostream> 3 using namespace std; 4 //make the pointer point at the 5th element of an array 6 int main) 7 int x[10]; 9 cin x[i]; 10 11 int*ptr; 12 13 14 cout <<*ptr << endl; 15 1 #include 3 using namespace std; 4 //make the pointer point at the 5th element of an array 6 int main) 7 int x[10]; 9 cin x[i]; 10 11 int*ptr;...