Question

it is a pointer operation and how to fix it? #include <stdio.h> int main(){    int...

it is a pointer operation and how to fix it?

#include <stdio.h>

int main(){
   int a[10]={2,5,8,1,4,7,3,10,6,9};
   int *MaxPtr, *MinPtr;
   MaxPtr=&a[0];
   MinPtr=&a[0];
   for (int i=1;i<10;i++){
       if ((*a+i)>*MaxPtr)
       {
           *MaxPtr=*(a+i);
       }
   }
   for (int c=1;c<10;c++){
       if (*(a+c)<*MinPtr)
       {
           *MinPtr=*(a+c);
       }
   }
   printf("Value of the item MaxPtr pointing at: %d\n",*MaxPtr);
   printf("Value of the item MinPtr pointing at: %d\n",*MinPtr);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* You are getting wrong result because you are referencing the two pointers to the same array and changing them according to the loops, now when the one pointer is changed correspondingly its values is also copied to the second pointer therefore what you can do either declare second array and copy the content of the first array to it and link second pointer to it or either print the max value before calculating the min value like in the following way */

#include <stdio.h>

int main(){
int a[10]={2,5,8,0,4,7,3,10,6,9};
int *MaxPtr, *MinPtr;
MaxPtr=&a[0];
MinPtr=&a[0];
for (int i=1;i<10;i++){
if (*(a+i)>*MaxPtr)
{
*MaxPtr=*(a+i);
}
}
printf("Value of the item MaxPtr pointing at: %d\n",*MaxPtr);
for (int c=1;c<10;c++){
if (*(a+c)<*MinPtr)
{
*MinPtr=*(a+c);
}
}

printf("Value of the item MinPtr pointing at: %d\n",*MinPtr);
}

Add a comment
Know the answer?
Add Answer to:
it is a pointer operation and how to fix it? #include <stdio.h> int main(){    int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • computers. 1. How many times will this loop repeat? include <stdlib.h> #include <stdio.h> void main(void) int...

    computers. 1. How many times will this loop repeat? include <stdlib.h> #include <stdio.h> void main(void) int 10 for(= 0; i < 101) printf("d", 1); A. 10 times B. 1 time C. It will run forever D. 0 times 2. What will be the output of this program? #include <stdlib.h> #include <stdio.h> int main() int a = 100, b = 200, C = 300; if(!a >= 500) b = 300; C = 400; printf("%d, 8d, &d", a, b, c); return 0;...

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

  • All in C please~ #1 Consider this program: #include <stdio.h> int main () { /* main...

    All in C please~ #1 Consider this program: #include <stdio.h> int main () { /* main */ constint constant1 = 10, constant2 = 20, constant3 = 14; int input_value; char current_truth; printf("What is the input value?\n"); scanf("%d", &input_value); current_truth = input_value > constant1; printf("current_truth = %d\n", current_truth); current_truth = current_truth && (input_value < constant2); printf("current_truth = %d\n", current_truth); current_truth = current_truth && (input_value == constant3); printf("current_truth = %d\n", current_truth); } /* main */ What is the output of this program...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • Having an issue with pointers and functions. #include <stdio.h> int f(int *a, int *b); int main()...

    Having an issue with pointers and functions. #include <stdio.h> int f(int *a, int *b); int main() { int a = 2, b = 7; b = f(&b, &a); printf("a = %d,\n", a); printf("b = %d\n", b); return 0; } int f(int* a, int* b) {     (*a) = (*a) + 3;     (*b) = 2*(*a) - (*b)+5;     printf("a = %d b = %d\n", *a, *b);     return(*a)+(*b); } can someone explain to me why the output is a =...

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • solve for the blank (make sure use C++) #include<stdio.h> int main() { int *ptr, *a; int...

    solve for the blank (make sure use C++) #include<stdio.h> int main() { int *ptr, *a; int b, c; int d[10]; b = 10 + 6; c = 10 + 1; ptr = &c; a = &b; for (b = 0; b < 10; b += 1) {     d[b] = b + 2; } //*ptr = ____ //*a = ____ //b = ____ //c = ____ //d = ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ b =...

  • #include <stdio.h> #include <stdlib.h> int aaa(); int main() { int ddd,eee = aaa (ddd,eee); printf("new num...

    #include <stdio.h> #include <stdlib.h> int aaa(); int main() { int ddd,eee = aaa (ddd,eee); printf("new num 1 = %d, new num2 = %d.\n", ddd,eee); return 0; } int aaa(int bbb,int ccc) {    bbb = 111;    ccc = 222;    printf("num 1 = %d, num 2 = %d.\n", bbb,ccc);    return bbb ,ccc;   } Output: num 1 = 111, num 2 = 222. new num 1 = 0, new num2 = 222. ************************************************************** I am learning function in C....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT