Jump to content
Sign in to follow this  
encik pot pet

Aktiviti Pengaturcaraan C

Recommended Posts

jawapan awak boleh post dalam thread ni..

1. dgn menggunakan 'nested loop'/gelung bersarang, paparkan

a)

$

$$

$$$

$$$$

$$$$$

cool.gif

F

FE

FED

FEDC

FEDCB

FEDCBA

c)

    A
   ABA
  ABCBA
 ABCDCDA
ABCDEDCBA

d)

*****

****

***

**

*

2. Rafi ada duit RM 3.74, untuk masukkan ke dalam tabung, dia kena tukar dalam bentuk duit syiling. Sila kira bilangan syiling 50sen, 20sen, 10sen, 5sen, 1sen yang Rafi dapat?

input: float

output: integer2 utk bilangan syiling

------------

sekian... sila post code awak masing

p/s:

ada soalan2 yang telah dipetik dari buku.. kalau dah buat tu..post jer

Share this post


Link to post
Share on other sites

a)

#include <stdio.h>

main() {

	int i, j;

	for (i=0; i<5; i++) {

  for (j=0; j<=i; j++) {

      printf("$");
  }

    printf("\n");
   }
}

yang lain lain tu aku penah buat kecuali b ngan c, tapi dah lupa sad.gif

yang d tu output aku tak cukup satu asterik kat bawah skali blink.gif

p/s bagus gak test camni.. hehehe.. hidup zeph (ayat ala ala manga)

Share this post


Link to post
Share on other sites
// uwaa.. exercising my mind, lama lew tak sentuh
// salam ceria 
// /me listen Raihan - Haji Menuju Allah\n

int menaik(const char w, const int n)
{
	int i, j;

	for(i = 0; i < n; i++) {
  for(j = 0; j <= i; j++)
  	printf("%c",w);
  printf('\n');
	}
	return n < 1 ? 1 : 0;
}

// yehaa, malas nak layan, kalau ada error :P
int menaiklainmacam(char w, const int n)
{
	char j;
	const char i = w - (char)n, k = w; // ?

	if(!isalpha(i))
  return 1;
	for(;w >= i; w--) {
  for(j = k; j > w; j--)
  	printf("%c", j);
  printf('\n');
	}
	return 0;
}

int menurun(const char w, int n)
{
	int j;

	for(;n > 0; n--) {
  for(j = 1; j <= n; j++)
  	printf(w); // 
  printf('\n');
	}
	return n < 1 ? 1 : 0; // ?
}[/code]

Edited by raihan

Share this post


Link to post
Share on other sites

A'kum. ni aku try jawab no 2 tuh.

Jawapan 1

#include<stdio.h>
#include<conio.h>

main()
{
 float duit, sen;
 int limapuluh=0, duapuluh=0, sepuluh=0, limasen=0, satusen=0;

 clrscr();

 printf("Masukkan duit : RM ");
 scanf("%f", &duit);

 sen =  duit * 100;
 printf("\nSen awal : %.0f", sen);

 do
 {
  if(sen >= 50)
  {
	limapuluh++;
	sen = sen - 50;
  }
  else if(sen >= 20)
  {
	duapuluh++;
	sen = sen - 20;
  }
  else if(sen >= 10)
  {
	sepuluh++;
	sen = sen - 10;
  }
  else if(sen >= 5)
  {
	limasen++;
	sen = sen - 5;
  }
  else if(sen >= 1)
  {
	satusen++;
	sen = sen - 1;
  }
  else
	printf("\nRalat");

 }while(sen != 0);


 printf("\n50 sen : %d", limapuluh);
 printf("\n20 sen : %d", duapuluh);
 printf("\n10 sen : %d", sepuluh);
 printf("\n5 sen  : %d", limasen);
 printf("\n1 sen  : %d", satusen);
 printf("\nsen    : %.0f", sen);

}
yang ni lak aku guna tatasusunan. Jawapan 2
#include<stdio.h>
#include<conio.h>

main()
{
 float duit, sen;
 int duitpecah[5]={50, 20, 10, 5, 1};
 int bahagiduit[5]={0, 0, 0, 0, 0};

 clrscr();

 printf("Masukkan duit : RM ");
 scanf("%f", &duit);

 sen =  duit * 100;
 printf("\nSen awal : %.0f", sen);

 for(int x=0; x<5; x++)
 {
	while(sen >= duitpecah[x])
	{
  bahagiduit[x]++;
  sen = sen - duitpecah[x];
	}
 }

 for(int y=0; y<5; y++)
  printf("\n%d sen : %d", duitpecah[y], bahagiduit[y]);

 printf("\nsen    : %.0f", sen);

}

Aku try gitu-gitu jer. Kot2 ada spa2 nak try run, kot ada error.

Share this post


Link to post
Share on other sites

aku best

kod u ni mungkin best skit kalau gunakan mod(%)

dan kurangkan penggunaan gelung happy.gif, kan. cuba lagi.

pastu biasa lengkapkan int main(... dgn pulangan return integer (4bytes).

tak nak pun takpe, tak salah~

Share this post


Link to post
Share on other sites

Ni cara utk aku buat soalan 1 c. tp cam panjang sgt. ada idea utk perbaiki kh? blink.gif

#include <stdio.h>

int main(int argc, char *argv[])

{

int i = 0, j = 0, k = 0;

char chr = 'E';

for(i = 4; i >= 0; i--) {

  for(j = 0; j < i; j++)

  printf(" ");

  for(j = 4; j >= i; j--)

  printf("%c", chr - j);

  for(k = 2; k >= j; k--)

  printf("%c", ((chr - j) + k) - 4);

  printf("\n");

}

return 0;

}

Share this post


Link to post
Share on other sites

XtremgenX

cuba u tengok camner contoh penggunaan printf("%[initial point]s",text); saya.

mungkin u leh pendekkan lagi koding u tu.

#include <stdio.h>

#define INITIAL "20"

int main(int argc, char *argv[])
{
        char buf[BUFSIZ];
        char *text = "hello";

        sprintf(buf, "%s", text);
        printf("%"INITIAL"s\n",buf);

        sprintf(buf, "%s%s", buf, text);
        printf("%"INITIAL"s\n",buf);

        return 0;
}

Share this post


Link to post
Share on other sites

raihan

smile.gif makseh.. ni coding terbaru yg aku buat ikut idea engko.

#include <stdio.h>

#define INITIAL "40"

int main(int argc, char *argv[])
{
	char buf[BUFSIZ], *chr = "A";

	for(int i = 0; i <= 4; i++) {
  sprintf(buf, "%s%c", buf, *chr + i);
  printf("%"INITIAL"s", buf);
  for(int k = 1; k <= i; k++)
    	printf("%c", (*chr + i) - k);
    printf("\n");
	}

	return 0;
}

dua loop awal tu dh hilang dah..

Share this post


Link to post
Share on other sites

1(a) Some info regarding the printf function...

You write %s to generate a sequence of characters from the values stored in the argument C string..... The conversion generates no more than p characters, up to but not including the terminating null character.

yang maner p refer kepada precision field. So let just solve 1(a) this way :

#include <stdio.h>

char str[] = "$$$$$";
int i = 0;

void main() {
    while (++i < sizeof(str)) printf("%.*s\n", i, str);
}
1(B) Benda yang sama apply, just change
char str[] = "$$$$$";
to
char str[] = "FEDCBA";
1(d) Same concept as 1(a) and 1(B), but now
#include <stdio.h>

char str[] = "*****";
int i = sizeof(str);

void main() {
    while (i-- > 0) printf("%.*s\n", i, str);
}
2.
#include <stdio.h>

float fwang;
unsigned sen50, sen20, sen10, sen5, sen1;

unsigned div(unsigned num, unsigned denom, unsigned *remainder) {
    *remainder = num % denom;
    return num/denom;
}

void main() {
    printf("Wang (dalam format ringgit.sen) : ");    
    scanf("%f", &fwang);
    {
    unsigned wang = fwang*100;
    sen50 = div(wang, 50, &wang);
    sen20 = div(wang, 20, &wang);
    sen10 = div(wang, 10, &wang);
    sen5 = div(wang, 5, &wang);
    sen1 = wang;
    }
    printf("\nSen\tBilangan\n===\t========\n");
    printf("50 \t%d\n20 \t%d\n10 \t%d\n5  \t%d\n1  \t%d\n", sen50, sen20, sen10, sen5, sen1);
}

Share this post


Link to post
Share on other sites

soalan seterusnya:

1) camna nak cari nombor perdana?

#include <stdio.h>

main()

{

int n, i=1, j, c;

clrscr();

printf("Enter Number Of Terms");

scanf("%d",&n);

while(i<=n)

{

c=0;

for(j=1;j<=i;j++)

{

if(i % j == 0)

c++;

}

if(c==2)

{

printf("%d",i)

i++;

}

getch();

}

can someone tell me what is the wrong with this code?

2) Camna:

input: 5

output:

1

12

123

1234

12345

rolleyes.gif

p/s: aku baru belajar C. Sampai skrg aku tak tau..diharap pakar sekalian membantu

Edited by cade

Share this post


Link to post
Share on other sites

soalan 2 tau la..

#include<stdio.h>

void main()

{

int num,row,col;

printf("Please enter one integer from 1 to 5:");

scanf("%d",&num);

for(row=1;row<=num;row++)

  {

  printf("\n");

  for   (col=1;col<=row;col++)

   printf("%d",col);

  }

return;

}

soalan 2 tu okla.. yg 1 xtau le.. try wat dpt 1eror lg.. xtau ktna..

aku pn bru je blaja C ni..

Edited by castlevama

Share this post


Link to post
Share on other sites

aku nak tnya arr.. aku berminat nak belajar c ni.. tapi dlm perkembangan skrg ni.... bukanke programming lebih kepada gui?... apa pendapat anda?..err.. dlm keadaam camner yang kita memerlukan programming dlm konsole?.. camner nak cakap ar.r... iss.. korang faham ke apa yang aku cuba sampaikan ni?.. hehehe

Share this post


Link to post
Share on other sites

aku nak tnya arr.. aku berminat nak belajar c ni.. tapi dlm perkembangan skrg ni.... bukanke programming lebih kepada gui?... apa pendapat anda?..err.. dlm keadaam camner yang kita memerlukan programming dlm konsole?.. camner nak cakap ar.r... iss.. korang faham ke apa yang aku cuba sampaikan ni?.. hehehe

awak jangan pandang sebelah mata program berbentuk console...

banyak jugak program yg lebih sesuai berbentuk console berbanding gui'ed dry.gif

jikalau awak tak berminat utk programming console dalam c, anggapla ianya utk belajar bahasa C dari segi syntax, structure, semantic, etc

and... ==> http://www.winprog.org/tutorial/

Share this post


Link to post
Share on other sites

2) Camna:

    input: 5

    output:

1

12

123

1234

12345

rolleyes.gif

for (i=1;i<nilai;i++)
{
for(j=1; j<=i;j++)
 {
   printf("%d",j);
 }
printf("\n");
}

Edited by cade

Share this post


Link to post
Share on other sites

2) Camna:

    input: 5

    output:

1

12

123

1234

12345

for (outval = inval; outval > 0; outval--) {
   printf ("%d\n", outval);
}

coding ni lagi simple kot tapi tak tau la leh run ke tak sebab aku wat ni kat cc, takde compiler. ade sesape nak tolong run kan ? kot kot ade error ke smile.gif

>_<

Edited by hotfloppy

Share this post


Link to post
Share on other sites

for (outval = inval; outval > 0; outval--) {
   printf ("%d\n", outval);
}

tanpa mengompile terlebih dahulu aku dah nampak error kat

printf ("%d\n", outval);

p/s: kalau kat cc, download dev-c++..kejap jer

Share this post


Link to post
Share on other sites

lain sikit smile.gif

  char str[6] = "12345";
  char* str2;
  for(int i=1; i<6; i++)
  {
    str2 = new char[i+1];
    strncpy(str2, str, i);
    str2[i] = '\0';
    cout << str2 <<endl;
  }

Share this post


Link to post
Share on other sites

ha'a la, salah kaw kaw ... hehehhe smile.gif aku kat kolej skang ni, dah try compile, humang aii... logic error tongue.gif

Share this post


Link to post
Share on other sites

lalala~ lama x masuk cnie lah..

 char str[6] = "12345";
 char* str2;
 for(int i=1; i<6; i++)
 {
   str2 = new char[i+1]; // best la pebuatan ko kt sini, geget karang, x boleh tau.
   strncpy(str2, str, i);
   str2[i] = '\0';
   cout << str2 <<endl;
 }
        int i;
        char str[7] = "1""\0""2345";
        for(i=0; i<5;i++) {
                cout << str <<endl;
                str[i+1] = str[i+2];
                str[i+2] = 0;
        }}
        int i, j;
        const int userinput = 5;
        for(i=1; i<=userinput; i++) {
                for(j=1; j<=i; j++)
                        cout << j;
                cout << endl;
        }

Edited by fatah

Share this post


Link to post
Share on other sites

        int i;
        char str[7] = "1""\0""2345";
        for(i=0; i<5;i++) {
                cout << str <<endl;
                str[i+1] = str[i+2];
                str[i+2] = 0;
        }}

cis.. yg ni terer siyuuutt.. dahsyat2... ph34r.gif

Share this post


Link to post
Share on other sites

wohh...Gila la korang...cam kod FBI pon ada...heheheh

aku pon nak try gak aaa...(baru seminggu blajar).... + curi sket skill En. Fatah...sebab kat 3 jam try dia jadik lain ...silap kat sini ( yg last : cout<<y<<endl;) so hangin je aku tgk....

BTW ni jwp aku...sempat wat yg ni jek.... nanti try yg lain la

{

for (int x=1;x<6;x++){

for (int y=1;y<=x;y++)

cout<<y;

cout<<endl;

}

Edited by Vibrunnica

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...