Garis besar-ruhut-paradaton-parsadaan-raja-sitorus-dohot-boruna-se-jabodetabek-1
RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
Transcript of RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
1/14
Tugas Kecil Syarat UTP
Oleh
Brian Arnesto Sitorus
1517051113
JURUSAN ILMU KOMPUTER
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS LAMPUNG
2015
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
2/14
Pointer
1.What is a pointer? How is it declared for an integer?
2.An integer variable x has the value 50. Write the code for declaration of the variable its
reference by name myx and pointer by name ptrx.
3. Write a test program to declare a float variable y, initialize it to zero. Create a reference
by
name refy. Now change y to 3.675. Find the value of refy and its memory location.
4. What are the sizes (in bytes) of memory spaces allocated for pointers to (i) a character
variable,(ii) a long int, (iii) a float number, (iv) double number.
5. Write code for declaring pointer to (i) one dimensional array, (ii) a two dimensional
array.
6. How do you get the value of variable from its pointer to pointer?
7. Write a program to declare an int variable and its pointer and to determine its square
and cube using its pointer.
8. How do you declare pointer to pointer for (i) int variable, (ii) for double variable.
9. What is wrong with the following codes.
(i) int* ptrn = n;
(ii) double ptrnPI = 3.14159;
(iii) char * ch = A ;
10. What is difference between a reference and a pointer?
11. Declare a reference with name ref_grade for character grades like A, B, etc
12. What do you understand by the following.
(i) int (*Function)(int, int) ;
(ii) double(*Function[])(int, int, double)
13.
14. What is the difference between following declarations?
(i) const int*ptr ;
(ii) int * const ptr;
(iii) const int * const ptr;
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
3/14
15. PA is a pointer to an array A[10]. Which of the following codes are correct to get the
nextelement of the array?
16. Write a program for illustrating the use of pointers for traversing an array
17. Write a test program to illustrate the application of oprators new and delete for int
variables
and for character variable.
18. When new[] is used to declare an array of integers, how do you initialize the elements
and
write code for their output and their deletion.
19. Write code of a program for preparing lists of names by user of program wherein the
total
number of entries are unknown. Make use of pointers.
20. Write a program to illustrate the application of new[] and delete[]
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
4/14
C-String
1.What are similarities and differences between arrays and C-string?
2.What is the difference between “A” and ‘A’ in C++?
3.Declare a pointer to a string
4.What is the difference between cin.get() and cin.getline()?
5.How do you code the above two functions in a program to read up to 20 characters?
6.Make a program that read user’s message like “Madan Mohan went to Mangalore on
Monday” and counts the number of ‘e’,’M’ and number of total characters.
7.What is the difference between cout.put and cin.putback()?
8.What does cin.peek do?
9.Explain the differences between function strchr() and strrchr().
10.What do you understand by function strncat(S1,S2,unsigned int n)?
11.What do you understand by function strncpy(S1,S2,n)?
12.What does the following codes do with two strings with name S1, and S2 ?
i.memcpy(S1,S2, size_t n);
ii. strcpy(S1,S2);
iii. strcat (S1,S2)
13.Give any two function belonging to standard character handling library. How do you
use them?
14.How do you convert a string of digits into an integer value?
15.What the functions atol() and atof() do to a string digit?
Atol() mengkonversi string digit ke tipe data long , sedangkan atof() mengkonversi string
digit ke tipe data double.
16. What is the difference between the function strchr(Str, int C) and strrchr(Str, int C)?
17.What do you understand by function isupper ( ) and tolower ()?
18.Explain the working of function cin.ignore(). Make a program with cin.ignore (4) to
illustrate its working.
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
5/14
19.Make a small program to illustrate the application of strlen().
20. Make a program in which a user is asked to enter his/her name.the program shouldget it verified by displaying the name and asking the user to enter yes if correct and no if
incorrect.
21. Make a program which asks the user to enter one or more sentences and counts the
numbers of ‘e’ in the sentences.
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
6/14
JAWABAN
Section 1-4
1.Pointer adalah variable yang menampung alamat dari variable lain.
Cara deklarasinya dengan menyebutkan tipe data int lalu diikuti operator tidak langsung
(*) dan nama variable pointer diakhiri (;).
2. #include
using namespace std;
int main()
{
int x=50;
int &myx=x;
int *ptrx=&x;return 0;
}
3. #include
using namespace std;
int main()
{
float y=0;
float &refy=y;
y=3.675;
cout
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
7/14
int main()
{
int A [2][2] = {1,2,3,4};int (*ptrA)[2] = A;
return 0;
}
6. Dengan menggunakan deference operator 2 kali (**), tanda (&) tetap 1 kali.
Contoh : int **ptrx=&ptrx;
7. #include
using namespace std;
int main(){
int x=2;
int *ptrx=&x;
int square,cube;
square=*ptrx**ptrx;
cube=*ptrx**ptrx**ptrx;
cout
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
8/14
11. char grades;
char &ref_grade= grades;
12.
13.
14. i.ptr bukan constant pointer dengan int yang konstan
ii.ptr adalah kosntan pointer dengan dengan int bukan konstan
iii.pointer konstan dan int konstan
15.
16.#include
using namespace std;
int main()
{ int A [] = {80};
int *ptrA = A;
cout
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
9/14
for (int i = 0; i < n; ++i) {
cout
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
10/14
P2 += 1;
}
return 0;
}
20. #include
using namespace std;
int main () {
char name[50];
char ch;
cout
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
11/14
JAWABAN
1.Persamaan: sama-sama ditampung dalam blok memori.Perbedaan:
- C-string diperlakukan sebagai suatu kesatuan, sedangkan array terpisah.
- Ketika nama dari array dipanggil,output akan menghasilkan alamat dari elmen
pertamanya, tapi C-string akan menampilakan string lengkap sebagai outputnya
2. ‘A’ merupakan tipe data char, sedangkan “A” merupakan tipe data string. Ketika string
diinisialisasi dengan string karakter, sistem menambahkan null karakter diakhir string.
3. Char *S= “You are Welcome to C++!”;
4. Cin.get() hanya dapat digunakan untuk 1 kali dan juga dapat membaca karakter lebih pendek, sedangkan cin.getline() dapat digunakan berkali-kali dan juga lebih banyak
membaca karakter.
5. #include
using namespace std;
int main()
{char S[20];
cout
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
12/14
++ total ;
if ( kata == ‘e’)
++e;if ( kata == ‘M’)
++m;
if ( kata == ‘.’)
break; }
cout
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
13/14
17. Isupper() bernilai true jika input/output huruf capital.
Tolower () mengembalikan huruf kapital menjadi huruf kecil.
18. #include
#include
using namespace std;
int main()
{
Char a;
Cout
-
8/16/2019 RUANG03 1517051113 Brian Arnesto Sitorus TUCIL
14/14
if(kata[i]=='e')
++hitung;
}cout