What does this print to the screen?
#include <iostream>
using namespace std;
int main ()
{
int *a;
a = new int[2];
for(int i = 0; i < 2; i++)
*(a + i) = i;
for(int i = 0; i < 2; i++)
cout << a[i] << " ";
delete [] a;
return 0;
}
I'm getting 0 1
Please step me through the process.
First of all we declare a pointer a. Now we allocate it memory equal to an array size of 2.
This means that we can now store integers at (a + 0) and (a + 1) locations.
Now in the first for loop, we initialise this new memory in the following way:-
*(a + i) = i for i = 0, 1
This means that store value of i at the memory location (a + i).
Thus, now (a + 0) points to an integer value 0 and (a + 1) points to an integer value 1.
Now *(a + i) , i.e., value at position (a + i) can also be written as a[i]. Thus the next for loop prints value at a and a + 1 locations.
Thus, a[0] = *(a + 0) = 0 and a[1] = *(a + 1) = 1
Thus the output is:-
0 1
What does this print to the screen? #include <iostream> using namespace std; int main () {...
What does this print to the screen? #include <iostream> using namespace std; int main() { int c = 5; int *p = &c; *p *= c; cout << *(++p); return 0; } I get 0 in Xcode, but it looks to me like it should be giving me the memory location. Please step me through the process.
One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << ; cout << endl; return 0; }
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...
This program illustrates dynamic variables #include <iostream> using namespace std; int main () { int *p1; p1 = new int; // Variables created using the new operator are called dynamic variables cout << "Enter an integer \n"; cin >> *p1; *p1 = *p1 + 7; cout << << "Your input + 7 = " << *p1 << endl; delete p1; // Delete the dynamic variable p1 and return the memory occupied by p1 to...
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n =0; int i = 0; cout << "Please enter a strictly positive number:"; cin >> n if (n <= 0) exit(EXIT_FAILURE) while (n > 1) n-n/2; i << endl; cout"Output:" return 0; Answer the following questions: What is the output of the program for each of the following values of n: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? What does...
in c++
#include <iostream> using namespace std; int main() int beta[7] = 3, 5); for (int i = 2; i < 7; i++) beta[i] = 3 * i + 2; beta[i - 1] = beta[i - 1] + beta[i]; beta[i - 2) = betali - 2] + beta (i - 1]; for (int i = 0; i < 7; i++) cout << beta[i] << " "; cout << endl; return 0;
#include <iostream> using namespace std; int main(void) { int SIZE; cout<<"Enter the size of the array"<<endl; cin>>SIZE; int *numlist = new int[SIZE]; // Read SIZE integers from the keyboard for (int i = 0; i<SIZE; i++ ) { cout << "Enter value #" << i+1 << ": "; cin >> numlist[i]; } // Display the numbers in a reverse order for (int i = SIZE; i > 0; i--...
#include <iostream>
using namespace std;
int * newZeroArray(int size) {
//your code here
}
int main() {
int size = 10;
int * A = newZeroArray(size);
for(int i = 0; i < size; i++)
cout << A[i] << " ";
cout << endl;
}Write a function that takes a size, creates a new array of that size with all zeros, and returns the array. If the size is not a valid size for an array, do not return a valid...
Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: "; cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2; return 0; }