2013-05-02 96 views
0

我在下面的代碼中遇到了一些麻煩;我知道這對於更有經驗的編碼人員來說是最基本的東西,但這是我第一次嘗試用C編碼,所以請耐心等待。功能:變量替代

#include <stdio.h> 

int main() 
{ 
    printf("This program, or function, substitutes one given number for another given number, and then places the former with the latter number. "); 

    float y1 = 10; /* Assign value to variable 1*/ 
    float y2 = 20; /* Assign value to variable 2*/ 
    float y3 = 30; /* Assign value to variable 3*/ 
    float y4 = 40; /* Assign value to variable 4*/ 

    void sub(float *, float *, float *, float *);     /*declare function prototype*/ 

    printf("\nThe numbers' given values before the aforementioned function: Number 1 = %f , Number 2 = %f\n , Number 3 = &f , Number 4 = %f", y1, y2, y3, y4); 
    sub(&y1, &y2, &y3, &y4); /*This is where the function, to move the numbers, is called */ 
    printf("\nThe numbers' given values after the aforementioned function: Number 1 = %f , Number 2 = %f\n , Number 3 = &f , Number 4 = %f", y1, y2, y3, y4); 
} 

void sub(float *z1, float *z2, float *n3, float *n4) 
{ 
    int z; 
    int n; 
    z = *z1; 
    *z1 = *z2; 
    *z2 = z; 
    n = *n3; 
    *n3 = *n4; 
    *n4 = n; 
} 

,輸出是:

This program, or function, substitutes one given number for another given number, and then places the former with the latter number. 

The numbers' given values before the aforementioned function: Number 1 = 10.000000 , Number 2 = 20.000000 , Number 3 = &f , Number 4 = 
30.000000 

The numbers' given values after the aforementioned function: Number 1 = 20.000000 , Number 2 = 10.000000 , Number 3 = &f , Number 4 = 
40.000000 

我的問題是:如何更改代碼,以便「數字3」替代品本身就像變量的其餘部分。

+1

提示:啓用突出顯示和編譯器警告。 – soon 2013-05-02 04:47:28

+1

你的'sub()'函數會交換數字,接受'float'值。但是你的臨時變量'z'和'n'被聲明爲'int'。 – steveha 2013-05-02 04:48:30

+0

@ steveha:我不會認真對待你的代表中的一小部分。我看到你的答案沒有錯,特別是它增加了比典型評論更多的洞察力。 – 2013-05-02 04:53:04

回答

3

您只需一個錯字:改變& F到%F爲

數3 = &˚F

0

麻煩的是與你的printf。請在您的printf語句中將號碼&f更改爲%f

+0

好的,謝謝!這有助於我加載! – The10thDoctor 2013-06-15 23:11:36