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(); } 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 Тролль Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now