msadu Posted September 30, 2009 Report Share Posted September 30, 2009 #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<iostream.h> void main() { float x; int i=2; const a=2.1; x=1; while (x<=a) {x=x+1/i; i++; } printf("x=%f", x); getch(); } Quote Link to comment Share on other sites More sharing options...
Тролль Posted September 30, 2009 Report Share Posted September 30, 2009 (edited) msadu: Во-первых, тип a неизвестен. Тип у константы можно не указывать в Паскале, но не в C++. Далее, цикл будет бесконечным, поскольку 1/i всегда будет равно ровно 0 - это деление целых чисел с отбрасыванием дробной части. Для исправления достаточно заменить выражение 1/x на 1./x И, хотя это не мешает выполнению, включено много неиспользуемых программой заголовочных файлов. Итак, #include<stdio.h>#include<conio.h>main(){ float a=2.1,x=1;for(int i=2;x<=a;i++)x=x+1./i;printf("x=%f", x); getch();} В будущем используй при вставке в сообщение текста программы тег "код" (кнопка с решеткой), иначе текст может исказиться. Edited September 30, 2009 by Тролль Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.