עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

שאלה ספציפית ב-C (רמה קלה)

Featured Replies

פורסם

אני רק מתחיל לתכנת ב-C. (אני לא יודע על יותר מדי פקודות)

אני מנסה לפתור את השאלה הבאה:

סדרה a1, a2, של מספרים מקיימת

an+1 = 3an an-1

עבור n >= 2. כתוב תכנית שתקבל כקלט את הערכים של a98 ו-a100 ותדפיס את ערכיהם של a1 ו-a2.

וזה מה שכתבתי:

הבעייה שאינני יודע לבדוק האם אני צודק.

א. עניתי נכון לשאלה?

ב. איך אני יכול לבדוק את עצמי?

#include <iostream>
using namespace std;
int main()
{
double a[100]={0}; //vector of the series numbers.

cout <<"enter a100 and a98:" <<endl;
cin >>a[99] >>a[97]; //load 2 numbers.
a[98]=(a[99]+a[97])/3; //calculation a99.
for (int i=96;i>=0;i--) //calculation a97 to a1.
{
a[i]=(3*a[i+1]-(a[i+2])); //example: a97=3a98-a99
}
cout <<"The a2 is: " <<a[1]<<endl;
cout <<"The a1 is: " <<a[0];
cout <<endl;
return 0;
}

פורסם

עושה רושם שהתוכנית שלך עובדת, אבל למה להשתמש במערך? אתה צריך לזכור רק שני ערכים כל פעם. אני אמנם לא יודע C, אבל זה אמור להיות קוד יעיל יותר:

#include <iostream>
using namespace std;
int main()
{
double y =0.0; //An+2 will receive A100
double x =0.0; //An+1
double temp =0.0; //will receive A98
cout <<"enter a100 and a98:" <<endl;
cin >>y >>temp; //load 2 numbers.
x=(y+temp)/3; //calculation of a99.
for (int i=97;i>0;i--) //calculation a97 to a1.
{
temp=(3*x-y);
y=x;
x=temp;
}
cout <<"The a2 is: " <<y<<endl;
cout <<"The a1 is: " <<x;
cout <<endl;
return 0;
}

תבדוק טוב את התנאי עצירה של הלופ, אם יש סיכוי ליפול זה שם.

פורסם

התנאי עצירה בסדר(התחלת מ- 97 ולא מ-96 כמו מקודם בגלל שלא כללת 0, בכל מקרה זה רץ 97-1+1=96-0+1=97 פעמים).

פורסם
  • מחבר

תודה רבה! :xyxthumbs:

אגב, מאיפה הצבעים בכתיבת הקוד? לי יש רק כחול, שחור וירוק. (והם נעלמים שאני מעתיק את הקוד לפורום)

פורסם

אתה מסמן את כל הקוד ואז:

[attachment deleted by admin]

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.