0% found this document useful (0 votes)
73 views14 pages

RUANG03 1517051113 Brian Arnesto Sitorus TUCIL

This document contains a list of 20 questions related to pointers in C++. It begins with basic questions about what pointers are and how they are declared. It then covers additional topics like pointer arithmetic, pointers to arrays, pointers to pointers, references vs pointers, and use of operators like new and delete with pointers. The purpose of the document is to provide sample questions to test knowledge of pointers for a small assignment in a Computer Science course at Universitas Lampung, Indonesia.

Uploaded by

Brian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views14 pages

RUANG03 1517051113 Brian Arnesto Sitorus TUCIL

This document contains a list of 20 questions related to pointers in C++. It begins with basic questions about what pointers are and how they are declared. It then covers additional topics like pointer arithmetic, pointers to arrays, pointers to pointers, references vs pointers, and use of operators like new and delete with pointers. The purpose of the document is to provide sample questions to test knowledge of pointers for a small assignment in a Computer Science course at Universitas Lampung, Indonesia.

Uploaded by

Brian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Tugas Kecil Syarat UTP

Oleh
Brian Arnesto Sitorus
1517051113

JURUSAN ILMU KOMPUTER


FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS LAMPUNG
2015

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;

15. PA is a pointer to an array A[10]. Which of the following codes are correct to get the
next
element 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[]

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 users 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.

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 should
get 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.

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<iostream>
using namespace std;
int main()
{
int x=50;
int &myx=x;
int *ptrx=&x;
return 0;
}
3. #include<iostream>
using namespace std;
int main()
{
float y=0;
float &refy=y;
y=3.675;
cout<<"nilai refy adalah "<<refy;
cout<<"\nalamat refy adalah "<<&refy;
return 0;
}
4. i.4
ii.4
iii.4
iv.4
5. i.
#include <iostream>
using namespace std;
int main()
{ int A [] = {1,2,3,4};
int *ptrA = A;
return 0;
}
ii.
#include <iostream>
using namespace std;

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 <iostream>
using namespace std;
int main()
{
int x=2;
int *ptrx=&x;
int square,cube;
square=*ptrx**ptrx;
cube=*ptrx**ptrx**ptrx;
cout<<"x adalah \n"<<*ptrx;
cout<<"luas adalah "<<square;
cout<<"\nvolume adalah "<<cube;
return 0;
}
8. i.
int n;
int *ptrn=&n;
int **pptrn=&ptrn;
ii.
float n;
float *ptrn=&n;
float **pptrn=&ptrn;

9. i.tidak ada reference operator (&) sebelum n.


ii.tidak ada tanda arsetik (*) sebelum nama pointer
iii.nama pointer tidak boleh sama dengan nama variable.
10. Reference merupakan nama lain dari suatu variable, sedangkan pointer menyimpan
alamat dari variable lain.

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 <iostream>
using namespace std;
int main()
{ int A [] = {80};
int *ptrA = A;
cout <<"*ptrA = "<<*ptrA <<" , \tptrA = "<<ptrA<<endl;

return 0;
}
17. #include <iostream>
#include <cstring>
using namespace std;
int main() {
int n;
char ch[50];
cout << "Enter total number of students: ";
cin >> n;
float* ptr;
ptr = new float[n];
cout << Enter the University name. ;
cin.getline(ch,50);
cout << "Enter GPA of students." <<endl;
for (int i = 0; i < n; ++i) {
cout << "Student" << i+1 << ": ";
cin >> *(ptr + i);
}
cout << "\nDisplaying GPA of students." << endl;

for (int i = 0; i < n; ++i) {


cout << "Student" << i+1 << " :" << *(ptr + i) << endl;
}
delete [] ptr;
return 0;
}

18. #include <iostream>


#define max 5
using namespace std;
int main(){
float *P1;
int *P2;
P1 = new float; //mengalokasikan satu ruang memori
//dan disimpan ke pointer P1
*P1 = 3.14; //mengisikan nilai ke dalam ruang
//yang telah dialokasikan
//menampilkan nilai dan alamat yang disimpan ke pointer P1
cout<<"Nilai *P1 : "<<*P1<<endl;
cout<<"Nilai P1 : "<<P1<<endl<<endl;
//mengalokasikan 5 buah ruang memori dan disimpan ke pointer P2
P2 = new int[max];
//mengisikan nilai ke dalam ruang - ruang memori yang telah dialokasikan
for ( int c=0 ; c<5 ; c++ ){
*P2 = (c+1) * 10;
P2 += 1;
}
//mengembalikan pointer P2 agar menunjuk ke alamat dari elemen ke-0
P2 -= 5;
//menampilkan nilai dan alamat yang disimpan kepointer P2
for ( int c=0 ; c<5 ; c++ ){
cout<<"Nilai *P2 ke - "<<c<<" : "<<*P2<<endl;
cout<<"Nilai P2 ke - "<<c<<" : "<<P2<<endl<<endl;

P2 += 1;
}
return 0;
}

20. #include <iostream>


using namespace std;
int main () {
char name[50];
char ch;
cout << "Enter your name : ";
cin.getline(name,50);
cout << "Your name is "<< name <<", Right? (y/n)";
cin >> ch;
if (ch == 'n'){
cout << "I'm sorry.";
}
else if (ch == 'y'){
cout << "Thanks for your respond :)";
}
else
cout << "Just y or n. Not the other";
return 0;
}

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 <iostream>
using namespace std;
int main()
{char S[20];
cout<<masukkan nama : ;
cin.get(S,20) ;
cout <<nama Anda adalah :<< S<<endl;
}
#include <iostream>
using namespace std;
int main()
{
char B[20];
cout <<Masukkan nama: ;
cin.getline(B,20) ;
cout<<nama Anda adalah = <<B <<\n ;
return 0 ;
}
6. #include <iostream>
using namespace std;
int main()
{
int total =0, e = 0,m=0;
char kata;
cout<<masukkan beberapa kata-kata \n ;
while (cin.get(kata))
{if ( kata != )

++ total ;
if ( kata == e)
++e;
if ( kata == M)
++m;
if ( kata == .)
break; }
cout <<total kata adalah = <<total <<, jumlah e adalah = <<e<<jumlah M
adalah<<m<<endl;
return 0;
}
7. Cin.putback() untuk mengganti suatu karakter menjadi karakter lainnya,sedangkan
cout.put
8. Cin.peek memperhatikan karakter,apabila karakter yang diinginkan itu ditemui, ia akan
memberikan sinyal,tetapi fungsi itu tidak melakukan apapun kepada fungsi tersebut,
melainkan sinyal itu dipakai untuk proses di fungsi lainnya.
9. Strchr() adalah fungsi yang mengembalikan pointer ke kejadian awal dari C di string,
sedangkan strrchr mengembalikan pointer ke kejadian akhir di C.
10. Fungsi yang menggabungkan karakter dari S2 ke S1, dengan n adalah batas
penggabungan S2.
11. Fungsi ini menggantikan karakter n pertama dari S1 dengan karakter n pertama dari
S2 dan menggembalikan S1
12. i.n karakter pertama dari S2 disalin ke n karakter pertama dari S1. S2 tidak berubah.
ii.Menyalin S2 ke S1, sehingga S1 adalah tiruan dari S2.Jika S1 lebih panjang dari S2,
maka ukuran S1 akan berkurang menjadi S2.
iii.menggabungkan S2 ke S1
13. Fungsi islower() dan toupper().
Cara memakai nya untuk islower() jika ada karakter yang huruf kecil kita dapat
menggunakan fungsi ini untuk memeriksanya.
Fungsi toupper kita gunakan jika mau mengubah huruf kecil ke huruf kapital
14. Dengan menggunakan fungsi atoi() di C++.
15.(ii) *(PA+1) ;
16. Strchr() adalah fungsi yang mengembalikan pointer ke kejadian awal dari C di string,
sedangkan strrchr mengembalikan pointer ke kejadian akhir di C.

17. Isupper() bernilai true jika input/output huruf capital.


Tolower () mengembalikan huruf kapital menjadi huruf kecil.
18. #include<iostream>
#include<cstring>
using namespace std;
int main()
{
Char a;
Cout<<masukkan huruf: <<endl;
While(cin.get(a))
{cin.ignore(4)
}
Cout<<a;}

Return 0;
}
Program ini akan mengabaikan tiap 4 karakter dari karakter pertama.
19. #include<iostream>
#include<cstring>
using namespace std;
int main()
{
char x[]={"Brian"};
cout<<"x adalah "<<x<<endl;
cout<<"panjang dari x adalah "<<strlen(x)<<endl;
return 0;
}
Program tersebut dapat menghitung jumlah karakter dari x dengan mengabaikan null
karakter.Sehingga Brian berjumlah 5 karakter.
20.
21. #include<iostream>
#include<cstring>
using namespace std;
int main()
{
int hitung=0;
int i;
char kata[50];
cout<<"masukkan beberapa kata: ";
cin.get(kata,50);
for (i=0;i<50;i++){

if(kata[i]=='e')
++hitung;
}
cout<<"ada "<<hitung<<" karakter 'e'"<<endl;
return 0;
}

You might also like