int main int a10 forint i 0 i SolutionIn the following pro
Solution
In the following programs I have heighlited the code where you can see the error / logical error/ compilation error etc.
Provided solutions in the form of comments to make you understand clearly.
1)
int main()
{
int a[10]; // You have declared an array with the size 10
for(int i=0;i<20;i++) // But here you are trying to access the 10,11,12....19 index\'s elements. This leads to a Garbage memory problem and run time error.
a[i]=10;
cout<< a[0] << a[1] << endl;
}
2)
int main()
{
int i=1;
if(i=0) cout << \"I is 0\"<< endl; // Compilation fails = is assignment operator so use == boolean operator
else cout<< \"I is 2\"<< endl;
}
3)
int main() // Program compiles and runs successfully,
{
int *a=new int[20];
for(int i=0;i<20;i++)
a[i]=i; // Considers i value as a pointer here is a logical problem to address perticular value
a=new int[10];
for(int i=0;i<10;i++)
a[i]=i;
delete [] a; // releasing a
}
4)
int main()
{
int 2i,j; // Compilation error, variable should not begin with numbers
for(j=0;j<10;j++)
{
cout<<j<<endl;
}
}
5)
int main() // Program compiles and runs but prints nothing
{
char i,j;
i=0.1; // This is not a correct statement you have to store a single character into a char data type variable
cout<< i<<endl;
}
6)
int main() // Program compiles and runs
{
int i,j,k;
if(i==0)
{
j=i+k; // Here program uses initial values to calculate j that is 0
}
cout<<j<<endl; // So prints 0 here
}
![int main() { int a[10]; for(int i = 0; i SolutionIn the following programs I have heighlited the code where you can see the error / logical error/ compilation int main() { int a[10]; for(int i = 0; i SolutionIn the following programs I have heighlited the code where you can see the error / logical error/ compilation](/WebImages/37/int-main-int-a10-forint-i-0-i-solutionin-the-following-pro-1112550-1761590246-0.webp)
![int main() { int a[10]; for(int i = 0; i SolutionIn the following programs I have heighlited the code where you can see the error / logical error/ compilation int main() { int a[10]; for(int i = 0; i SolutionIn the following programs I have heighlited the code where you can see the error / logical error/ compilation](/WebImages/37/int-main-int-a10-forint-i-0-i-solutionin-the-following-pro-1112550-1761590246-1.webp)