① c編程練習題
這個問題很簡單。
首先你要知道函數的參數傳遞的只是副本。
調用函數swap(int *p1,int *p2) ,只是對函數實參數的地址副本進行地址交換,就著這個函數來說,在交換前,p1地址是pointer_1,p2的地址是pointer_2;交換後p1地址是pointer_2,p2的地址是pointer_1,但pointer_1和pointer_2的地址還是沒變還是原來的地址,通俗說,a,b形參分別拿著A、B兩實參的鑰匙的復印件,a、b交換了鑰匙,但A、B並沒有交換鑰匙,這里的鑰匙就是指的是地址!
如果你想讓pointer_1和pointer_2的值改變,就改寫swap(int *p1,int *p2)。
swap(int *p1,int *p2)
{int *p;
*p=*p1;
*p1=*p2;
*p2=*p;
}
這個函數通俗說,a,b形參分別拿著A、B兩實參的鑰匙的復印件打開A、B的房門將A、B倆房間里的東西對調,雖然A、B並沒有交換鑰匙但卻交換了房間里的東西!
② c語言編程訓練
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static void guess(int number, int *count)
{
int n;
do
{
scanf("%d", &n);
if ( n > number )
{
printf("Your answer is HIGHER, try again.\n");
}else if ( n < number )
{
printf("Your answer is LOWER, try again.\n");
}
++*count;
}while ( n != number );
}
int main(int argc, char *argv[])
{
int number;
int count = 0;
srand((unsigned int) time(NULL));
while ( count < 15 )
{
number = rand()%100 + 1;
guess(number, &count);
if ( count <= 7 )
{
printf("congratulation.\n");
}
else if ( count > 7 && count <15 )
{
printf("You can do it better.\n");
}
}
printf("I can』t bare it.\n");
}
③ C語言編程練習
#includeint main(){ double s; int a[10000]; int i,n,d; while(~scanf("%d",&n)) { d=0; s=0; for(i=0; i=60) d++; printf("average = %.1lf\ncount = %d\n",s,d); } return 0;}
④ c語言編程練習題1
#include
"stdio.h"
main()
{
double
a=0,b;
int
i;
for(i=1;;i++)
{
printf("Enter
a
number:");
scanf("%lf",&b);
if(b>a)
a=b;
if(b<=0)
break;
}
if(a>=0)
printf("%lf\n",a);
}
經驗證,float數據不夠精確,如輸入100.62,輸出的卻是如100.620003。所以用了更精確的double數據。希望能幫到你。
⑤ c語言編程:練習題。
#include<iostream.h>
int main()
{
int i,j,t;
int a[10],b[10];
cout<<"請輸入第一個數組:"<<endl;
for(i=0;i<10;i++)
cin>>a[i];
for(i=1;i<10;i++)
for(j=0;j<9;j++)
if(a[i]<a[j])
{t=a[i];a[i]=a[j];a[j]=t;}
cout<<"排序後的第一個數組為:"<<endl;
for (i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"請輸入第二個數組:"<<endl;
for(j=0;j<10;j++)
cin>>b[j];
for(i=1;i<10;i++)
for(j=0;j<9;j++)
if(b[i]<b[j])
{t=b[i];b[i]=b[j];b[j]=t;}
cout<<"排序後的第二個數組為:"<<endl;
for (i=0;i<10;i++)
cout<<b[i]<<" ";
cout<<endl;
for(i=0;i<10;i++)
a[i]=a[i]+b[i];
cout<<"兩數組的和為:"<<endl;
for (i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
⑥ 請問應該怎麼學好編程呢c語言好難,有什麼練習方法嗎
1.需要耐心
2.需要記住程序代碼
3.練習的最佳方法就是去編程序 自己去編程序
4.最好的方法是你自己要把它學好 並堅持克服其中的枯燥 到最後的學成
⑦ 如何能在自己的電腦上運行C語言,自己進行簡單的編程訓練
當然是編譯工具咯如果你想有TurboC2.0的話,去 http://www.programfan.com/的工具下載里下載。但我推薦你Dev-C++,因為它是WINDOWS操作界面,並且支持標准C語言,而且是32位的程序,即使是64位的雙核CPU都可以運行。但你說的那本書里的代碼卻不符合標准,在這個軟體又用不了,所以你要考慮一下。如果你電腦真的用不了TurboC2.0的話,可以裝個虛擬機來虛擬一個DOS或WINDOWS95/98也行。
⑧ c語言編程練習
float*calcSize(floatheight,floatweight,intage)
{
floathatSize=0;//帽子尺寸
floatcoatSize=0;//上衣尺寸
floatwaistSize=0;//腰圍尺寸
floatfact1=2.9;
floatfact2=0.125;
floatfact3=0.1;
floatresult[4]={0};
if(height<0||weight<0||age<0)
{
cout<<"inputerror!"<<endl;
returnNULL;
}
hatSize=(weight/height)*fact1;
coatSize=height*weight/288;
if(age>30)
{
coatSize+=fact2*((age-30)/10);
}
waistSize=weight/5.7;
if(age>28)
{
waistSize+=fact3*((age-28)/2);
}
result[0]=hatSize;
result[1]=coatSize;
result[2]=waistSize;
returnresult;
}
⑨ C語言編程練習。
三個循環,加上
if (122 * a + 212 * b + 221 * c == n)
{
}
判斷條件就行
⑩ c語言編程練習題
第一個是輸入半徑,輸出圓的直徑、周長、面積
const double p = 3.14159;
void main()
{
double radius = 0;
printf("Radius of Circle:");
scanf("%lf", &radius);
printf("Diameter:%lf, Circumference:%lf, Area:%f ", 2*radius, 2*p*radius, p*radius*radius);
}
第二個是根據給定的余額和利息,計算一年以後的余額
void main()
{
double initBalance = 6000;
double interestRate = 0.0425;
printf("Interestcalculation program.\nStarting balance:%.lf Annual\ninterest rate percentage:%.4lf\n", initBalance,interestRate);
printf("One year balance:%.lf", initBalance*(1+interestRate));
}