2014-11-06 83 views
-2

分配問:C,如何將值存儲在for循環中,是否可以在沒有數組的情況下完成?

程序必須計算房子 的表面,用戶必須輸入他的房間數量,然後程序必須要求每個房間的lenght和witdh。之後,程序必須顯示房子的表面。沒有提供公式,但房間的表面是長寬*寬。

我的應用程序的問題是,在for循環中,每個房間的所有長度和寬度的總和的值不會被記住。所以輸出是錯誤的。 它可以在沒有陣列的情況下完成嗎?因爲我還沒有上過這個課,還有很多其他任務需要完成。 非常感謝。

#include <stdio.h> 
#include <stdlib.h> 

main() 
{ 
int camere,lungime,latime,scamere,i,scamera,scameratotal,scasa,total; 


printf("Acest program calculeaza suprafata casei\n"); //this program lists the surface of the house 
printf("Cate camere are casa?\n"); // how many rooms do you have? 
scanf("%d",&camere); 

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

printf("Introduceti lungimea si latimea camerei %d\n",i); //enter the lenght and width of room %d 

scanf("%d %d",&lungime,&latime); 


scamera=lungime*latime; //surface of room %d is lenght*width 

printf("Suprafata camerei %d este %d\n",i,scamera); //states the surface of room %d (1 , 2 or 3 etc.) 


total = total + (lungime*latime); // total that i want the program to remember 
scasa=total*camere;     //surface of the house is total*number of rooms 


} 

printf("\nSuprafata casei este de %d metri",scasa); //the surface of the house , bad output , it's not total(sum of surfaces of each room)*number of rooms 
} 
+0

是的,它可以完成沒有陣列。 – Sneftel 2014-11-06 14:58:55

+0

請縮進您的代碼。 – 2014-11-06 15:03:08

+0

謝謝大家,最後我的配方沒有了。 – 2014-11-06 15:44:05

回答

0

聲明另一個的int稱爲總

在你的for循環結束時更新總

total += lungime*latime; 

總在循環之後將在年底的總建築面積

+0

試過了,在這裏發佈舊版本。它提供了不好的輸出。 – 2014-11-06 15:04:37

+0

編輯代碼,請看看。 – 2014-11-06 15:24:01

+0

'scasa = total * camere;''不需要。 printf在最後應該打印''total'' – Vorsprung 2014-11-06 16:57:38

1
int total = 0; 

for(i=0;i<camere;i++) 
{ 
//ask length 
//ask width 
total = total + (length * width); 
} 

display total 

編輯:

#include <stdio.h> 
#include <stdlib.h> 

main() 
{ 
    int length = 0; 
    int width = 0; 
    int rooms = 0; 
    int total = 0; 

    printf("Acest program calculeaza suprafata casei\n"); 
    printf("Cate camere are casa?\n"); 
    scanf("%d", &rooms); 

    for(int i=0;i<rooms;i++) 
    { 
     printf("Introduceti lungimea si latimea camerei %d\n",i); 
     scanf("%d %d",&length,&width); 
     total = total + (length * width); 
    } 

    printf("Suprafata casei este de %d square meter", total); 
} 

如果這行不通,也許有在使用scanf函數的錯誤,但從來沒有使用過

+0

試過了,它給出了不好的價值。 – 2014-11-06 15:18:43

+0

當你聲明它時總是賦值,因爲你永遠不知道它存儲在哪個內存空間中,如果這個內存空間是空的,並且給你的代碼提供空間,在一行中聲明10個變量是在中間丟失的最好方法的邏輯,也米*米=平方米;) – Charlie 2014-11-06 15:34:30

+0

@Charlie請在答案中提交格式化的代碼。 – 2014-11-06 15:38:42

0

您的代碼顯示出你剛剛第一printf之前做初始化的變量計算未定義的行爲。這裏是你的代碼與所有的變化解釋:

#include <stdio.h> 
#include <stdlib.h> //unwanted header 

int main() //use int main 
{ 
    int camere,lungime,latime,scamere,i,scamera,scameratotal=0,scasa; //some unused variables here! 

    //scamera=lungime*latime; 
    //scameratotal+=scamera; Undefined behaviour here! 

    printf("Acest program calculeaza suprafata casei\n"); 
    printf("Cate camere are casa?\n"); 
    scanf("%d",&camere); 

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

     printf("Introduceti lungimea si latimea camerei %d\n",i+1); //room numbers start from 1 not 0 
     scanf("%d %d",&lungime,&latime); 
     scamera=lungime*latime; 
     scameratotal+=scamera; // add each scamera 
     printf("Suprafata camerei %d este %d\n",i+1,scamera); //i+1 here too 
    } 

    //scamera=lungime*latime; 
    //scasa=scamera*camere; unwanted lines here! 

    printf("Suprafata casei este de %d metri",scameratotal); //i think you need to display total surface area 
    return 0; //main returns int 
} 
+0

請在答案中提交格式化的代碼。 – 2014-11-06 15:39:05

+0

謝謝,我的配方沒有了。 – 2014-11-06 15:44:22

+0

@MichaelWalz,我不知道爲什麼,但是當我在這個特定的答案中點擊編輯時,OperaMini不會做任何事情。無論如何,現在我已經改進了我的電腦的格式。 – 2014-11-07 11:19:34

相關問題