C Programming, Help with the homework Instructions: You are to turn in a text fi
ID: 3741552 • Letter: C
Question
C Programming, Help with the homework
Instructions: You are to turn in a text file that includes all output from the program as well as all of the variable values at the requested locations
The code generated "should" all be valid, but if there are any lines that access arrays beyond their size, you are to correct the errors by
changing the invalid values to the maximum value that would be allowed.
So in other words, if x is a size 10 array and your homework includes the following line:
x[11] = 14;
you should change this to:
x[9] = 14;
And make a note in the document you turn in that you made this change.
Edit: No comments should be typed
Code:
#include
int func2(int a, int *b)
{
int c;
c = a;
a = *b + c;
printf("func2: %d %d %d ",a, *b, c);
return(c);
}
int func2_3(int *a, int b)
{
int c;
c = *a;
*a = b + c;
printf("func2_3: %d %d %d ",*a, b, c);
return(c);
}
int func2_34(int *a, int b)
{
int c;
c = *a;
a = &c;
*a = b + c;
printf("func2_34: %d %d %d ",*a, b, c);
return(c);
}
int main()
{
int *ptr, *a;
int b, c;
int d[10];
b = 10 + 7;
c = 10 + 2;
ptr = &b;
a = &b;
for (b = 0; b < 10; b += 1)
{
d[b] = b + 4;
}
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
b = 4 + 10;
c = 3;
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
for (c = 0; c < 10; c += 2)
{
printf("%d ",d[c]);
d[c + 1] = c / 2 + 9;
}
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
a = &c;
for (c = 0; c < 10; c += 2)
{
printf("%d ",d[c]);
d[c + 1] = c / 2 + 9;
if (c % 4 == 0) { printf(" "); }
}
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
ptr = &d[4];
a = &d[3];
for (*a = 1; *a < 4; *a += 1)
{
ptr[*a] = *a + 1;
printf("%d %d ", *a, ptr[*a]);
}
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
printf("-- %d -- ", *d + 5);
printf("-- %d -- ", ptr[26 % 5] + 5);
func2(d[7], &d[4]);
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
func2_3(&d[7], d[4]);
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
func2_34(&d[7], d[4]);
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
d[2] = func2(d[7], &d[4]);
//*ptr = ____
//*a = ____
//b = ____
//c = ____
//d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
printf(" ");
return(0);
}
Explanation / Answer
Code:
#include<stdio.h>
int main()
{
int *ptr, *a;
int b, c;
int d[10];
b = 10 + 6;
c = 10 + 5;
ptr = &c;
a = &b;
for (b = 0; b < 10; b += 1)
{
d[b] = b + 6;
}
// *ptr=15
//*a=10
//b=10
//c=15
//d=1901419552
b = 4 + 9;
c = 2;
// *ptr=2
//*a=13
//b=13
//c=2
//d=1273426272
for (c = 0; c < 10; c += 2)
{
printf(" d[%d] :%d ",c,d[c]);
d[c + 1] = c / 2 + 10;
}
// *ptr=10
//*a=13
//a=1888455836
//c=10
//d=1888455792
a = &c;
for (c = 0; c < 10; c += 2)
{
printf(" chk d[%d] %d ",c,d[c]);
d[c + 1] = c / 2 + 10;
if (c % 4 == 0) { printf(" "); }
}
// *ptr=10
//*a=10
//b=13
//c=10
//d=1412216864
ptr = &d[4];
a = &d[3];
for (*a = 1; *a < 4; *a += 1)
{
ptr[*a] = *a + 1;
printf(" a=%d ptr[%d] =%d ", *a,*a, ptr[*a]);
}
//*ptr=10
//*a=4
//b=13
//c=10
//d=1288071072
printf("-- %d -- ", *d + 7);
printf("-- %d -- ", ptr[27 % 5] + 7);
return 0;
}
If any queries please get back to me
Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.