0 like 0 dislike
5.2k views

Use the pointers p1, p2, and p3 as a first number, the second number and the sum respectively and complete the following program to read two integers and display the sum of the integers between them. For example, if the user enters 6 and 10, the program should display 24 (7+8+9). The program should force the user to enter numbers less than 100 (<100) or greater than 0 (>0) and the first integer should be less than the second, else display "Invalid value".

Sample code:

#include <stdio.h>
int main(void){
    int *p1,*p2,*p3,i,j,sum;
    p1 = &i;
    p2 = &j;
    p3 = &sum;
    //Your code


    return 0;
}


Sample Input 1:

6 10

Sample Output 1:

24 (7+8+9)

Sample Input 2:

10 6

Sample Output 2:

Invalid value

 

[Exercise] Coding (C) - asked in Chapter 12: Pointers and Arrays
ID: 38252 - Available when: Unlimited - Due to: Unlimited

edited by | 5.2k views

58 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
int main(void)
{
* * ** ** * ****** ** *p1,*p2,*p3,i,j,sum,first=0;
    p1 = &i;
* ** ** * *** *** * = &j;
    p3 = &sum;
* * **** ** * * * ** *
* ** * * * *** * * * ******* * * * %d",p1,p2);
*** * * ** ** * * ****** || i==j || i+1==j || i<0 || j<0)
    {
* ** ** *** ** * **** **** *** ** ** * *** ** ** * ** * ** value");
    }
* * ** * * * ** * ** * *
    {
* *** ** ****** * * * * * * ** * * * * *** k;
*** *** ** * **** * ** ****** * * ** ****** ** *** * **** *** * *
* * ** **** * *** * *** ** * * ***** ***** * ** * *** * ** ******
** ** * **** * * ** * ** ** ** * * **
*** * * **** ** * ** * ** * ******* * ** * ",*p3);
*** *** * * * * **** * ** * * ****** * * * ***
* * * ** ** * * *** ** * * *** ** * * ** * ***** **** * *
** ** * ****** ** ********* * **** * *** *** *** *** ** (first==0){
** * ** ***** **** ***** * *** * * ** * **** ** *** **** * *** **
** *** * ** **** ** ** * *** * ****** * **** ** * ***
* * *** ** * ** * *** * * ** * * * ** * *** ** ** **** ** ** *
** ***** * *** ** * * ** ***** ** ** * * * ** * *** * *** ** * **** *** * * ** ** * * *** * *

* ** ** * ** * ** ** ** * ** ** * ** * * * *** ****
** * * * * ** ** **** * ** ** ** ** *** ** ** * * * * **** * * *** *** * * * * *
** ** ** ** *** * ** * **** **** * ** ** * *
******* ** ** * * *** ***** * * * * * * * * ** *****
    }
*** ********** *** **** ***** ** code
return 0;
}
answered by (-329 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include * * **
int main(void){
* * * ** * ******* * ** * ***** *** * *
*** * *** ***** * = &i;
** * **** ** ** * = ** ***
** ** * *** ** * *** * = ** ** *
** ** ** * * * *** ** *** ** > j)
****** ******* *** * *** *** *** * **** * * ** * * *
****** * **** * **** ** * < 0 || j < 0 || i < 100 || j < 100)
* *** ** * *** ** **** * * * ** ***** * * ** ** ** * * *
* *** ** * ** ** ***** = i+1 ; x < j ; x++)
** * ** ** ** ** *

** * ** * * ** * ** ***


***** * ** * * ****** 0;
}
answered by (-255 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
* ** * * **

void ******** a[], int n, int * *** int * * * * *

int * * * *
{
* *** * * ** int n, largest, ****
* ******* * ** * ****** ** * **** ** ** * *** will be entered
*** * * *** int a[n],i;
*** **** * ******* * * * n integers *** * by spaces
* **** *** * for (i = 0; i < n; i++)
* * * ** ** * ** ** * ** * ** ** * *

* * * * **** * * ***** n, * *** * * ****

** * * * * * if (n == 0)
****** *** * ** * * ***** * * numbers were * * ****
** * * *** ** else if (n == 1)
** *** **** ** ** ** **** * * one number was * * * ** *** ** ** ** *
*** else
* * ** ** ** * * * ** ** ** * * ** * %d, Second **** * * ** *** ***

**** * * return 0;
}

void *** * a[], int n, int * * ** int * * ** **
{
* * * ** * int i;
* ** **** * = a[0];
* * * ** ** ** * = 0;
** * * ** * for(i = 0 ; i < n ; i++)
* ** ***** *** *

* * * * * * *** * * ** * ** **** ** * * > **
*** * ****** ****** ** * * **
* * ** ** ** ** * * ** * * **** ** * ** *** ** = a[i];
* **** * * **** * * * ** ** * *** ******
* * *** ** * *** * * * * * *** ** = 0 ; i < n ; i++)
** ** ** ** * * * ** * *** * * *** ** *
** ** *** * ** **** ** * ****** * ** * ** > * **** * * *** *** a[i] < ** *
** ** ** ** * * * ** ** ** * *** * * * * * * * ****** * *** * *** *** = a[i];
** * * *** * *** * * **** **** * ** *** **

* ** ****** ** **** **
}
answered by (-284 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
void sum_of_pointers(int num1,int num2,int *sum);

int main()
{
* * * * * * * * * **** i,num1,num2,sum=0;
** * ** *** * *** ** *** ** * *** * ** ** ****** *** ** ** ** *
* *** * * ** * ** * *
    {
*** ** ** * * ** *** *** **** *** * ** * ****** * value");
*** * **** * * * * *
* *** *** *** *** ** * if(num1<0)
******* * * ** ** ** * ** *** * * * ** * ***
*** * * ** ** * * *** *** ** * ***** * * * ** * * ** * value");
** * *** *** ** * ** ** * * * * *** *** * *****
* *** ** * ** ** * * * * * if(num2>100)
* ** *** * ** *** * ** * *** * * ** ** *** **
******* * *** ** * * * **** ** * ** * * ** ** * ****** value");
* * * ***** * *** ***** * * ** * *****
* ** * * **** * ** ** ** ***
***** ***** ** *** ***** * ** * *** * ***
* * ** ** *** * * * **** * * ** ",sum);

** * *** ** * * ** ** b;
* * **** ** * * * * ** ** ** * ** **
** ** ** * *** * *** ** * ** * b<num2-1; b++)
* * * ** ***
* **** * ** ** *** * ** * * ** * * ** *** ** ** ** * *
* * ****** ** * *
*** ** * * ** *** * * *** * * *
}

void sum_of_pointers(int num1,int num2,int *sum)
{
* * ** * **** * ** * *** * i;
* ** * * **** *** ** * * ** ** *** * *
** ** * * ** *
** ** * * * * ** * **** * * * ** * * **** * *****
    }
}
answered by (-323 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include * * ** *** *
int main(void){
*** ** *** * **** ** *** ** **
* ***** * * *** ***** = &i;
** *** * ** ** * = &j;
**** *** * ***** *** ** ** = &sum;
** ***** ** * ***** * code
** *** * * ** ** *** * *** * ***
* ***** * **** * * *****
**** ** * * **
***** * * ****** *
if * * * || (*p1)<0 || * ***** || (*p2)<0 || * * *** * **
{
*** ** ** * ** * ** ** *** * * ** * ***** ****
}else {
* * * * * *** * ** * * * * ",*p3);

* * **** * *

int i;
* * * * ** ** * *
* * * *** * * * ***
{
** ***** ** * *** *
}


****** ** * *



}
*** * ****** * * **** *** 0;
}
answered by (-336 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main(void){
    int *p1,*p2,*p3,i,j,sum;
    p1 = &i;
    p2 = &j;
    p3 = &sum;
* * * *** ** * ** code
** * * * ***** **** ** * ** * * ** **** * *
* * ** * ** * * * * **** **** * * **** * * *** *** * *
* *** * * * * *** ****** * * *** * *** * **** ** value");
    else
    {
* * ** ** * ** * * ***** ** ****** ***** * n;
* * ****** *** ** ** ** * * ** * **** ** *
* ***** ** * * **** * ****** * ** **** a[n];
** * ***** *** * ** ***** * *** ** * * *** ***
** * *** **** * ** * * **** ** ** ****** k;
* ******* * * * * * **** *** *** *** * * * * * * * ** * * *
*** * * * ** ** ** * * * * * ** * *
***** ** * * **** *** * * ** * * * * ** ** ******* * ** ** ** * * ** * *
* ***** * ** *** *** *** ** * ** ** * * *** *** ** ***** ** ** * * **
** **** ** * * * ** ** ** ** *** **** ** ** **
* ** * * * *** * * *** * * ** ** * ** * ** *** * ***
*** * * **** ** * ***** * * * ** ** * * * *** *** (%d",a[0]);
* *** * ** * * ** ** * *** * ** * *** ** * * ****** *
** *** ** **** * * **** *** * *** * ** * *


** * * * ** *** ** * * ** * ** *** * ** * * ** * * ** ** *
* * * * ** * * * * * * * * * * * * * ** * *
*** * * ** * ** * ** * ** * ** ** ** ** ** ***** * ** * * ** **

    }



** * **** * * * * ** 0;
}
answered by (-196 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include ** ** * *
int main(void){
*** ***** **** * ** *** * * ***** * ** ** ** * *
** * * * ** * * ** = &i;
***** * *** * ** ** * = &j;
* *** * ****** * * ** ** = &sum;
*** **** * * ** ** ** * * ** * ** * *
* ** * * * *** ** (*p1<*p2 && *p1>0 && *p2<100)
*** * * * ** * *** * ***** * (*p1+1<*p2)
* ****** * * * *
*** ** ****** ***** * * *** ** * * ** * ** *** += *p1+1;
** ** * ** * ***** * * * ** * * * * ***
** **** ** * ** ** *
*** ** **** ** * * ** *** * * *** * ** * ****
* * * **** * * *** *
* * * * ****** * * * **** * * ** ** * ****** ** ** *** ** value");
* ** **** ******** *** ** ** 0;
}
answered by (-249 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
void sum_of_pointers(int num1,int num2,int *sum);

int main()
{
*** **** * ** * * * ** i,num1,num2,sum=0;
* ** * * ******** ** *** * * * *** ** * *** * * *
** **** * *** * *** ** **** ** * *** **
* ***** * ** * *
* * * * **** * ** ********** ** * ** ** *** **** *** value");
** * * *** * * *** *
***** * ** ** *** *
** * * * * *** * **** **** ** *** * * ***** **
* ** * ** ***** *** * * ***** * * * ",sum);

* ** *** * ***** * ** * * * b;
* * * ** ***** ** ** *** * ** *** * *
* ** * * * * *** ** ** * b<num2-1; b++)
* ** * * * ** *
** *** * * ** * * * ** *** *** ** * * ******** **** * * **** **
    }
* * * * * * ** * * *** * *** * **** *
}

void sum_of_pointers(int num1,int num2,int *sum)
{
* * * ** * *** ** ** * i;
* * ** ** *** *** * ** * ** **
***** **** * * *
* ** ** * **** * ** ****** * * *** * ** *
*** * * * ***** ** * **
}
answered by (-323 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main(void){
    int *p1,*p2,*p3,i,j,sum=0,n;
    p1 = &i;
    p2 = &j;
    p3 = &sum;
*** ** *** * *** ** * *** * ** %d" ,&i,&j);
    if(i>j)
****** *** ** * * *** *** * * * * ** **
*** ******** ***** ** * *** * ** * ** * ** ** ** * ** * * ** value");
* ** * * * * * ******** *** * ***
    else if(i<0||j<0||i>100||j>100)
** **** *** *** * *** *** * ** * ******
** * * * *** * * ** ** * * * * * * *** ***** * * * * **** value");
**** * * ** ** * ** ** ***
    else
    {
**** * * * * ** * * * * ** ** * *** **** * *** *
***** * * ** * ** * *** **** * ** * ** * ****
*** * **** * *** * ** ** * ****** *** * ***** * * * ** ** ** * * *
** ** ** *** *** ** * *** ***** *
** * * ****** *** ** ** * ** * * **** * *** ***** *** " ,sum);
* ** ** **** ** * *** * ** *** * ** *** * (j-i==2)
* * * *** ** * ** ** * ******* * ** * ** * ** ,i+1);
* * * **** * **** *** ** *
* * ***** * ** ***** ** * ** * * *
* * * * * ** * *** * ** *** ** * * * * * * * ** ** * * *** ** * * ***
* **** *** * ** ********** ** *** * ** * * ***** * * * *

** * * * * * * * * ** *** * ***** * * * * *** * ** * * * ****** * * ** *
* * ** **** * ** ****** *** * *** ** * * * * * *** *
*** ******* ** ** * *** ** * * ** *** * * ***** * * * * ** * ** * * **** ** ** ** * * * ** * * ** ***
* *** * ** ** * ** ** ** ** * * * **** ** *** ** * * **
* *** * ****** * ****** ** ** * ** *** ** **** ** ** **** ** ** * * if(n==j-1)
***** *** ** * *** * **** * * ** * ** ** * *** ** *****
* * ** * ** * * ** * ** * * * *** * **** * * ** * **** ** * ** * ** * **** *** * *** ** *** *** ** ** **
* *** * * **** *** * * * * ** * * * ** * * **** ** * * * *** * * * *
* * * * ***** * * * *** *** *** * ** * * ** ** * **** *** ***
* ****** * ** * * ** * * * * *** **** * ** * *** * ** * *** * * * * *** ** * ** * *** *** *
** ** ** * * *** ** ** ******* *** ** ** * * *** * *** **
** ** * *** ** * ** ** ** * * ***
    }
    return 0;
}
answered by (-32 points)
edited by
0 0
prog.c: In function 'main':
prog.c:33:5: error: expected declaration or statement at end of input
     }
     ^
0 0
prog.c: In function 'main':
prog.c:34:5: error: expected declaration or statement at end of input
     }
     ^
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Correct output
0 0
prog.c: In function 'main':
prog.c:6:10: error: stray '\342' in program
     p3 = \xe2\x88\x91
          ^
prog.c:6:11: error: stray '\210' in program
     p3 = \xe2\x88\x91
           ^
prog.c:6:12: error: stray '\221' in program
     p3 = \xe2\x88\x91
            ^
prog.c:6:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     p3 = \xe2\x88\x91
        ^
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Correct output
Case 4: Correct output
0 0
prog.c: In function 'main':
prog.c:6:10: error: stray '\342' in program
     p3 = \xe2\x88\x91
          ^
prog.c:6:11: error: stray '\210' in program
     p3 = \xe2\x88\x91
           ^
prog.c:6:12: error: stray '\221' in program
     p3 = \xe2\x88\x91
            ^
prog.c:6:8: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     p3 = \xe2\x88\x91
        ^
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main(void){
****** * * * * ***** *p1,*p2,*p3,i,j,sum;
*** *** * * *** ** * = &i;
* *** * ********** ** * * = &j;
*** * * * * * * = &sum;
* * * * * ** ** * * code
* ** * ***** * * count;
** **** **** * **** ** *
*** ** *** * * ** * ***** ** ** %d",&i,&j);
** * ****** ** * * * ** (*p1>*p2 || *p1<=0 ||*p2>=100)
* *** * * * ** *
** ** * * * ** **** * ** * * * ** * * ** * *** value");
* ********* ** * * ** **
** * * * ** ** **** ***
** * ** * *** ******
* ** *** * ** * *** * **** * * * *** ** ** * **** * *** **** ** *
* * * * ** * * * ** ** ** * ** ** ***** *** *** * **** * * * *** *
****** ** ** * * * ****** * * ** ** ** * ** * * **** ****** ** **** * *
**** * ** * * * ** ** *
***** **** * *** * * ** * * 0;
}
answered by (-249 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
Case 4: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.59.89
©2016-2024

Related questions

0 like 0 dislike
58 answers
[Exercise] Coding (C) - asked Dec 21, 2017 in Chapter 12: Pointers and Arrays
ID: 38251 - Available when: Unlimited - Due to: Unlimited
| 5.9k views
12,783 questions
183,443 answers
172,219 comments
4,824 users