Jump to content
Sign in to follow this  
edril

Camne Nak Panggil Array...tolong Ar Aku Pening

Recommended Posts

aku tengah wat progaming yang gunakan function calling.skang ni kat aku punye void main() aku wat satu koding yang bolehkan user input data,then stret away masuk dalam array yang aku bina guna nama num[8].skng ni ak nak wat satu function yg mana function tu leh baca data dari array , one by one.means dari [0] sampai la ke [7].then function tu leh tengok yang mana nilai maksimum and minimum.Soalan aku, nak guna koding ape yg bolehkan fuction yg aku nk wat neh detect nilai maksimum and minimum.?then mcm mana nak hantr parameter array yng ak buat kat void main tu ke dalam function?

soalan aku yang kedua, lets say yang bila fnction aku dah okey,then aku nak tambah plakfunction yng leh delete data dari array yg ak wat td.contohnya katelah aku nak delete data tu if nilai die lebih dari nilai 5.cane eh?

Share this post


Link to post
Share on other sites
cuba baca sini dulu. http://www.devarticles.com/c/a/Cplusplus/B...ions-in-C-plus/
kat bawah ni apa yang aku dapat dalam article tu.

CODE
#include <fstream.h>

int Add(int,int); //function prototype

int a,b,c;

int main()
{
   a=2;
   b=3;
   c=Add(a,b); //function call
   cout << c << endl;
   return 0;
}

int Add(int a, int b) //function definition
{
   c=a+b;
   return c;
}


1 - min/max patut guna if/else.
2 - hantar parameter, nampak tak camna dia hantar a & b kat c=Add(a,b ); //function call
3 - siapkan dulu 1 & 2 pastu ko boleh pikir pasal manu screen pulak.

p/s: baru aku perasan pasal void main() tu. so aku assume ko bercakap pasal C kan?
camna pun the basic dah ada kat atas tu, cuma in term of language je beza sikit. Edited by MatchMaker

Share this post


Link to post
Share on other sites
1) nak pass array ke function lain bole guna:
CODE
void sayaPanggilArray(int arrayKu[]){
    //apa-apa
}

int main () {
    int a[3] = {1,2,3};
    sayaPanggilArray(a); //pass array "a" ke function...actually cuma pass pointer item pertama dalam array
}


2) utk yang macam ko nak tu, bole buat contohnya camni:
CODE
#include <iostream>
using namespace std;

const SIZE = 7;

int minOrMax(int arr[], bool isMin){
    int value = arr[0];
    for(int i=0; i<SIZE; ++i){
        value = isMin? (arr[i]< value? arr[i]: value) : (arr[i]> value? arr[i]: value);
    }
    return value;
}

int main(){
    int x[SIZE] = {9,15,30,8,10,6,21};

    cout<<minOrMax(x,true) << "\n";
    cout<<minOrMax(x, false) << "\n";
return 0;

}


//oleh sebab ko tak bagipon apa yang ko dah buat, aku assume ko memang nak jawapan bukan penerangan...so aku tak akan terangkan contoh c0de yang aku bagi kat atas tu...harap maklum biggrin.gif
//tolong != tolong.buatkan

Share this post


Link to post
Share on other sites
#include <iostream>
using namespace std;

const SIZE = 7;

int minOrMax(int arr[], bool isMin){
int value = arr[0];
for(int i=0; i<SIZE; ++i){
value = isMin? (arr[i]< value? arr[i]: value) : (arr[i]> value? arr[i]: value);
}
return value;
}

int main(){
int x[SIZE] = {9,15,30,8,10,6,21};

cout<<minOrMax(x,true) << "\n";
cout<<minOrMax(x, false) << "\n";
return 0;

}

1) camne nak translate coding
cout<<minOrMax(x,true) << "\n";
cout<<minOrMax(x, false) << "\n";
kepada codng c.......

2) int minOrMax(int arr[], bool isMin)

bool isMin....untk pe eh..aku t familiar dengan bool.

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