មេរៀនទី១៤: អនុវត្តលំហាត់ Array


















មេរៀនទី១៤: អនុវត្តលំហាត់ Array

គណនា​ Address របស់​ Array តាម​ Column major order

C C++


សូម​​ជួយ​ប្រាប់​​ផងបើ​​ កម្ម​វិធី​ដែល​ខ្ញុំ​សរសេរ​មាន​កំហុស​!!

#include<iostream.h>
#include<conio.h>
void Pi(int p[],int i,int Ub[],int Lb[]);
void main()
{    int Lo,n,c;
char ch;
cout<<“********************\n”;
cout<<“*Column major order*\n”;
cout<<“********************\n\n”;
do
{
cout<<“Enter dimension:”;
cin>>n;
int *s=new int[n],*s1=new int[n];
cout<<“Enter c word:”;
cin>>c;
cout<<“Enter start address(Lo)=”;
cin>>Lo;
cout<<“Enter size of array from left to right(Ex:a[3][5][6]) :”;
for(int i=0;i
cin>>s1[i];
cout<<“Enter index that want to find:\n”;
for(i=0;i
{    cout<<“s["<<i<<"]=”;
cin>>s[i];
}
int *Lb=new int[n],*Ub=new int[n];
for(i=0;i
{    Lb[i]=0;
Ub[i]=s1[i]-1;
}
i=0;
int *p=new int[n];
i=n-1;
Pi(p,i,Ub,Lb);
int result;
result=Lo;
for(int j=0;j<=n-1;j++)
result=result+(p[j]*(s[j]-Lb[j])*c);
cout<<“The result is address:”<<result;
cout<<“\nPress r to restart or anykey to exit:”;
ch=getch();
clrscr();
}while(ch==’r’||ch==’R’);
}
void Pi(int p[],int i,int Ub[],int Lb[])
{    p[0]=1;
for(int k=1;k<=i;k++)
{  p[k]=1;
for(int j=k-1;j>=0;j–)
p[k]=p[k]*(Ub[j]-Lb[j]+1);
}
}
កូដ​អំពី​ការ​បោះ​លេខ​​ Random ដោយ​មិន​ជាន់​តំលៃ​គ្នា
វា​និង​ធ្វើ​ការ​ Random ពី​លេខ​១ ដល់​ r*c តែ​វា​ process យូរ​បន្តិច
#include<iostream.h>
#include<stdlib.h>
void main()
{    int b[100],e[100],a[10][10],t=0,j,i,r,c;
cout<<“Enter r:”;    cin>>r;
cout<<“Enter c:”; cin>>c;
for(i=0;i<100;i++)
b[i]=0;
for(i=0;i
{    again:
randomize();
e[i]=random(10000)%(r*c+1);
for(int j=0;j<=i;j++)
if(b[j]!=e[i])    t++;
else    goto again;
if(t<=i)
{  t=0;
goto again;
}
t=0;
b[i]=e[i];
}
int cre=0;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{    a[i][j]=e[cre];
cre++;
cout<<endl<<a[i][j];
}
}
បំលែង​លេខ​ពី​ Decimal ទៅ​ជា​ Binary ដោយ​ប្រើ​លក្ខណៈ​ Stack
C C++


សូម​បញ្ជាក់​ថា​ maxstack =50

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define maxstack 50
struct stack
{    int top;
int info[maxstack];
};
void initialize(stack *st)
{    st->top=-1;
}
int fullstack(stack *st)
{    return (st->top==maxstack-1);
}
int emptystack(stack *st)
{    return (st->top==-1);
}
void push(stack *st,int item)
{    if(fullstack(st))
cout<<“\nStack overflow”;
else
{    st->top=st->top+1;
st->info[st->top]=item;
}
}
int pop(stack *st)
{    int element;
if(emptystack(st))
{    cout<<“\nStack underflow”;
return 0;
}
else
{    element=st->info[st->top];
st->top=st->top-1;
return element;
}
}
void main()
{    int a[50],num;
struct stack st;
int pos;
char ch;
do
{  clrscr();
cout<<“Enter decimal number:”;
cin>>num;
pos=0;
while(num!=0)
{    a[pos]=num%2;
num=num/2;
pos++;
}
a[pos]=-1;
initialize(&st);
pos=0;
while(a[pos]!=-1)
{    push(&st,a[pos]);
pos++;
}
cout<<“Binary number:”;
while(st.top!=-1)
cout<<pop(&st);
cout<<“\nDo you want to restart?(Y/N):”;
ch=getch();
}while(ch==’y’||ch==’Y’);
}

បំលែងរយៈពេល ភាសាC program
C C++
//នេះជាកម្មវិធីសំរាប់ធ្វើការគណនារយៈពេល​ ម៉ោង នាទី រឺ វិនាទី ដែលយើងបានបញ្ចូល
//ទៅជាចំនួនសរុបនៃ នាទី រឺ វិនាទី រឺ ម៉ោង
#include<stdio.h>
#include<conio.h>
void main()
{
long int h,mn,c,d,f,g;
int e;
clrscr();
printf(“Enter 1 to Convert Hour to mn and s\n”);
printf(“Enter 2 to Convert mn to Hour and s\n”);
printf(“Enter 3 to Convert s to mn and Hour\n”);
printf(“Please enter the number=”);scanf(“%d”,&e);
clrscr();
if(e==1)
{    printf(“Input Hour=”);scanf(“%ld”,&h);
printf(“Input Minute=”);scanf(“%ld”,&mn);
c=(h*60)+mn;
d=c*60;
printf(“=>%ldh%ldmn=%ldmn=%lds”,h,mn,c,d);
}
if(e==2)
{    printf(“Input Minute=”);scanf(“%ld”,&mn);
c=mn/60;
d=mn%60;
f=mn*60;
printf(“=>%ldmn=%ldh%ldmn=%lds”,mn,c,d,f);
}
if(e==3)
{    printf(“Input s=”);scanf(“%ld”,&c);
mn=c/60;
h=mn/60;
d=mn%60;
g=c%60;
printf(“=>%lds=%ldh%ldmn%lds=%ldmn%lds”,c,h,d,g,mn,g);
}
if(e>3||e<1)
goto exit;
getch();
exit:
}
//បញ្ចប់

បង្ហាញរូបភាព ភាសា C
C C++

//នេះគ្រាន់តែជាការសរសេរលេងសំរាប់បង្ហាញរូបភាពជាការេដែលរីកពីតូចទៅធំតែប៉ុណ្ណោះ
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int x,i,j,nn,n,e,y,i1,j1,e1,k,color1,color2,w1,w2;
clrscr();
n=1;
while(!kbhit())
{
for(i=1;i<=n;i++)
{    gotoxy(x=40-i,y=0+i);
for(j=1;j<=i;j++)
{       color1=j;
textcolor(color1);
cprintf(“*”);
color1++;
}
for(e=1;e<i;e++)
{       color2=j;
textcolor(color2-2);
cprintf(“*”);
j–;
}
cprintf(“\n\r”);
}
k=1;
for(i1=i-1;i1>=1;i1–)
{
gotoxy(x+k,y+k);
for(j1=1;j1<=i1-1;j1++)
{       color1=j1;
textcolor(color1);
cprintf(“*”);
color1++;
}
for(e1=1;e1
{       color2=j1;
textcolor(color2-2);
cprintf(“*”);
j1–;
}
cprintf(“\n\r”);
k++;
}
delay(100);
n++;
if(n>=25)
{
n=1;
}
clrscr();
}
getch();
}
//ចប់

ចែកកត្តាបឋម ភាសា C
C C++
//នេះគឺជាcode សំរាប់​ធ្វើការចែកលេខជាកត្តាបឋម
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,d=2,n,a[20],j=2;
clrscr();
printf(“Enter number=”);scanf(“%d”,&n);
while(n>=d)
{
while(n%d==0)
{
a[i]=n/d;
printf(“%d³”,n);
printf(“%d\n”,d);
n=a[i];
i++;
j++;
}
d++;
}
printf(“1³”);
getch();
}

//បញ្ចប់