مشاهدة النسخة كاملة : حل اسئلة مراجعة مادة البرمجه ++c
السلام شباااااااب ,,,,,,
عندي لكم حل أسئلة المراجعه لمادة ++C
طبعاا الي تخصصه حاسب الي ونازله له مادة البرمجه هذا الترم يعرف عن ايش انا قاعد اتكلم ...
مو مشكله الكل بيعرف انشاء الله .. لاني سوف يقوم بوضع الأسئلة أولا ثم الاجوبه(الكود) .
اتمنى من الي عنده اي سؤال او استفسار او حل اخر انه يكتب السؤال ولكن بعد ان اكمل جميع الاجابات ....
Review Exercises
1-1) Write a program to output the odd numbers from 1 to n
note>>number from user
1-2) Do (1) but for numbers n to m
m ,m from user note>>
2) Write a program to calculate the sum and average of the numbers from from n to m
3) Compute the value of the series 1/2+1/3+1/4+1/5+1/6(put to 1000 tems)
4) Write a program to read 100 numbers and find the largest and second largest
5) Input in the number of seconds , output the number of hours ,minutes , seconds.
Example: 4000 second seconds=1hors+6mintes + 40 seconds.
6)Write a program to do a scientific calculator supporting addition ,subtactes, multiplication, division . Assume only two terms operations . Read input as chars and convert to numbers using "atof" used as : atof(x) .examples :input can be 2+9
7)Read a text and count the frequency of each in the text , Output each letter and its frequency in one line . Example "The C program is good " , output will include each letter and its frequency .
8)Do the above exercise by finding the highest frequency letter.
9)input 500 numbers in array ; find the highest occurrence of of x
10)input 500 numbers in array ; make each item in the array the sum of all items preceding it including itself .
11)input 100 numbers in array ;transpose the array I,e make the last element the first and the first element the last .
12)sort an array of 100 numbers .
13) input an array of 100 numbers ;find the number of times a number x has occurred in the array .
input 100 numbers in an array; put all odd number in a new array and even number in another new array.
1-1) Write a program to output the odd numbers from 1 to n
note>>number from user
#include <iostream.h>
#include <stdio.h>
int main()
{
int n, i=1;
cin>>n;
while(n>i)
{
cout<<i<<endl;
i+=2;
}
getchar();
return 0;
}
1-2) Do (1) but for numbers n to m
m ,m from user note>>
#include <iostream.h>
#include <stdio.h>
int main()
{
int n, m;
cin>>n>>m;
if(n%2==0){
n++;
}
while(m>n)
{
cout<<n<<endl;
n+=2;
}
getchar();
return 0;
}
2) Write a program to calculate the sum and average of the numbers from from n to m
#include <iostream.h>
#include <stdio.h>
int main()
{
int n, m , sum=0 ,average, count=0;
cin>>n>>m;
if(n%2==0){
n++;
}
while(m>n)
{
sum=sum+n;
count++;
n++;
}
average=sum/count;
cout<<" the sum is ="<<sum<<"the average is ="<<average;
getchar();
return 0;
}
3) Compute the value of the series 1/2+1/3+1/4+1/5+1/6(put to 1000 tems)
#include <iostream.h>
#include <stdio.h>
int main()
{
float c=2 ,o=0 ,n=0 ;
while(c<=1000)
{
n=n+1/c;
c++;
}
cout<<" value = "<<n;
getchar();
return 0;
}
4) Write a program to read 100 numbers and find the largest and second largest
#include <iostream.h>
#include <stdio.h>
int main()
{
int n, i=100 ,max ,secound;
cin>>n;
max=n;
secound=n;
while(i>=1)
{
cin>>n;
if(max<n){
max=n;}
if(secound<n&&n<max){
secound=n;
}
i--;
}
cout<<"the largest is"<<max<<endl;
cout<<"the secound largest is="<<secound<<endl;
getchar();
return 0;
}
5) Input in the number of seconds , output the number of hours ,minutes , seconds.
Example: 4000 second seconds=1hors+6mintes + 40 seconds.
#include <iostream.h>
#include <stdio.h>
int main()
{
int input , s , m , h;
cin>>input;
h=input/3600;
m=input/600;
s=input%60;
cout<<"hours is"<<h<<endl;
cout<<"minutes is="<<m<<endl;
cout<<"seconds is="<<s<<endl;
getchar();
return 0;
}
6)Write a program to do a scientific calculator supporting addition ,subtactes, multiplication, division . Assume only two terms operations . Read input as chars and convert to numbers using "atof" used as : atof(x) .examples :input can be 2+9
#include <iostream.h>
#include <stdio.h>
int main()
{
int i,j ,f;
char op;
cin>>i>>op>>j;
switch(op)
{
case '+':cout<<i+j;
break;
case '-':cout<<i-j;
break;
case '*':cout<<i*j;
break;
case '/':cout<<i/j;
break;
case '%':cout<<int(i)%(int)j;
break;
}
getchar();
return 0;
}
7)Read a text and count the frequency of each in the text , Output each letter and its frequency in one line . Example "The C program is good " , output will include each letter and its frequency .
#include <iostream.h>
#include <stdio.h>
int main()
{
char a[100] ;
int i ,g , d=0;
gets(a);
for(i=0;i<strlen(a);i++){
cout<<a[i];
d=0;
for(g=0;g<strlen(a);g++){
if(a[i]==a[g]){
d++;
}}
cout<<d;
cout<<"\n";
}
getchar();
return 0;
}
8)Do the above exercise by finding the highest frequency letter.
#include <iostream.h>
#include <stdio.h>
int main()
{
char a[100] ;
int i ,g , d , s=0 ,x;
gets(a);
for(i=0;i<strlen(a);i++){
cout<<a[i];
d=0;
for(g=0;g<strlen(a);g++){
if(a[i]==a[g]){
d++; }
if(s<d){
s=d;
x=i;}
}
cout<<d;
cout<<"\n";
}
cout<<"\n\n highest frequency letter "<<a[x];
getchar();
return 0;
}
9)input 500 numbers in array ; find the highest occurrence of x
#include <iostream.h>
#include <stdio.h>
int main()
{
int a[500] ;
int c ,o , u , n ,t ,x;
for(c=0;c<500;c++){
cin>>a[c];}
cout<<" Enter number to find ";
cin>>x;
for(o=499;o>=0;o--){
if(a[o]==x){
n=o;
}
}
cout<<" the highest occurrence is "<<n;
getchar();
return 0;
}
10)input 500 numbers in array ; make each item in the array the sum of all items preceding it including itself .
#include <iostream.h>
#include <stdio.h>
int main()
{
int a[500] ;
int i ,sum=0 , d , g;
for(i=0;i<500;i++){
cin>>a[i];}
for(d=0;d<500;d++){
sum=sum+a[d];
a[d]=sum;
}
for(g=0;g<500;g++){
cout<<endl<<a[g];}
getchar();
return 0;
}
11)input 100 numbers in array ;transpose the array I,e make the last element the first and the first element the last .
#include <iostream.h>
#include <stdio.h>
int main()
{
int a[100] ;
int i , d , g ,t , r;
for(i=0;i<100;i++){
cin>>a[i];}
for(t=99,d=0;d<50;d++,t--){
r=a[d];
a[d]=a[t];
a[t]=r;
}
for(g=0;g<100;g++){
cout<<endl<<a[g];}
getchar();
return 0;
}
12)sort an array of 100 numbers .
#include <iostream.h>
#include <stdio.h>
int main()
{
int a[100] ;
int i ,j , d , temp ;
// this section for input##
cout<<" Enter 100 numbers \n";
for(i=0;i<100;i++){
cin>>a[i]; }
// this section for sort array##
for(j=0;j<100;j++){
for(d=j+1;d<100;d++){
if(a[j]<a[d]){
temp=a[j];
a[j]=a[d];
a[d]=temp;
}}}
// this section for come out##
cout<<"\n the sort by largest is \n";
for(j=0;j<100;j++){
cout<<a[j]<<endl;
}
getchar();
return 0;
}
13) input an array of 100 numbers ;find the number of times a number x has occurred in the array .
#include <iostream.h>
#include <stdio.h>
int main()
{
int a[100] ;
int i ,j , d , x, count=0 ;
// this section for input##
cout<<" Enter 100 numbers \n";
for(i=0;i<100;i++){
cin>>a[i]; }
// this section for find occured##
cout<<"\n Enter number to find occured\n";
cin>>x;
for(d=0;d<100;d++){
if(x==a[d]){
count++;
}
}
cout<<" occured "<<x<<" is = "<<count;
getchar();
return 0;
}
14)input 100 numbers in an array; put all odd number in a new array and even number in another new array.
#include <iostream.h>
#include <stdio.h>
int main()
{
int a[100] , odd[100] ,even[100] ;
int i ,j , d , w=0 , f , g=0 ;
// this section for input##
cout<<" Enter 100 numbers \n";
for(i=0;i<100;i++){
cin>>a[i]; }
// this section for sort array##
for(w=0,i=0,j=0;j<100;j++){
if(a[j]%2==0){
even[w]=a[j];
w++;}
else {
odd[g]=a[j];
g++;}
}
// this section for come out even numbers##
cout<<"\n the even numbers is \n";
for(i=0;i<w;i++){
cout<<endl<<even[i]; }
// this section for come out odd numbers##
cout<<"\n the odd numbers is \n";
for(f=0;f<g;f++){
cout<<endl<<odd[f]; }
getchar();
return 0;
}
الحمد لله إنتهيت وبصراحة أجد صعوبة بالغة في محاولة تنسيق الموضوع :mad:
انتظر ردودكم. ملاحظه لست متأكد من (logic ) علما بأن هذه الاكواد تعطي نتيجه صحيحه 99%
طبعا الي يبي يجرب الاكواد لاينسى يغير الارقام .. يعني مو مقوله يبي يدخل 500 رقم ,,:confused:
تقـبـلو خـالـصـ أحـتـرامـي أخوكـــم الــكــونــــت ,,,:88:
αzzαм вιη αв∂υℓαzιz
01-02-09, 12:16 AM
الله يعطيك العافية أخوي الكونت
ماقصرت
سلمت يمينك على الجهد
الله يعافيك اخوي ( دعاني الشوق) ....
اآسف شباب هناك خطأ في الكود الرابع والخامس ,,,,
الكود الرابع يكون .... هكذا ...
#include<iostream.h>
#include<stdio.h>
main()
{
int x,max1,max2,i=2;
cout<<" Enter Number 1 = ";
cin>>x;
max1=x;
max2=x;
while(i<=5)
{
cout<<" Enter Number "<<i<<" = ";
cin>>x;
if (x>max1)
{
max2=max1;
max1=x;
}
else if (x<max1)
{
if(x>max2||max1==max2)
{
max2=x;
}
}
i++;
}
cout<<" The First Largest = "<<max1<<endl;
cout<<" The Second Largest = "<<max2;
getchar();
}
و الكود للسؤال الخامس يكون .... هكذا ...
#include <iostream.h>
#include <stdio.h>
int main()
{
int input , s , m , h;
cin>>input;
h=input/3600;
m=input%3600/60;
s=input%60;
cout<<"hours is= "<<h<<endl;
cout<<"minutes is= "<<m<<endl;
cout<<"seconds is= "<<s<<endl;
getchar();
return 0;
}
جزاك الله خير اخوي صالح على التنبيه ! ...
السلام عليكم
مشكور اخوي وماقصر الله يعطيك العافيه
بالنسبه للسؤال 2 عندي استفسار
2) Write a program to calculate the sum and average of the numbers from from n to m
انت حليته كذا
#include <iostream.h>
#include <stdio.h>
int main()
{
int n, m , sum=0 ,average, count=0;
cin>>n>>m;
if(n%2==0){
n++;
}
while(m>n)
{
sum=sum+n;
count++;
n++;
}
average=sum/count;
cout<<" the sum is ="<<sum<<"the average is ="<<average;
getchar();
return 0;
}
سؤالي انت ليه اضفت هذي الخطوه
if(n%2==0){
n++;
}
اذا كان n زوجي بتضيف عليه 1 اما اذا فردي ما يسوي شي؟!
انا على حسب فهمي للسؤال يقول ابيك تطبع المجموع والمعدل من n to m
منتظر ردك ...
وبالنسبة للسؤال الخامس
5) Input in the number of seconds , output the number of hours ,minutes , seconds.
Example: 4000 second seconds=1hors+6mintes + 40 seconds.
جرب تدخل 3600
المفروض الناتج بيكون 1 ساعه
لاكن هنا الناتج يقول 1 ساعه و6 دقايق !!!!!
انا حليته بطريق ثانيه بس فيها مشكله اذا ادخلت 3600 الناتج يكون 60 دقيقه المفروض يقول 1 ساعه
#include <iostream.h>
#include <stdio.h>
int main()
{
double x,y;
int h,m,s;
cout<<"enter number of seconds ?\n";
cin>>x;
y=x;
h=x/3600;
x=x/3600;
x=x-h;
m=x*60;
x=x*60;
x=x-m;
s=x*60;
cout<<y<<" seconds = "<<h<<" hours "<<m<<" mintes "<<s<<" seconds";
getchar();
return 0;
}
ممكن يكون الحل طويل لاكن هذا الحل اللي طرا على باللي اول ما قريت السؤال
اما انو يكتب 60 دقيقه بدل ساعه ولا يضيف 6 دقايق من عنده!!!!!!!!!!
وانا الحين الحل التمارين الباقي اذا فيها شي اقولك
تحياتي
السلام عليكم
مشكور اخوي وماقصر الله يعطيك العافيه
بالنسبه للسؤال 2 عندي استفسار
2) Write a program to calculate the sum and average of the numbers from from n to m
انت حليته كذا
#include <iostream.h>
#include <stdio.h>
int main()
{
int n, m , sum=0 ,average, count=0;
cin>>n>>m;
if(n%2==0){
n++;
}
while(m>n)
{
sum=sum+n;
count++;
n++;
}
average=sum/count;
cout<<" the sum is ="<<sum<<"the average is ="<<average;
getchar();
return 0;
}
سؤالي انت ليه اضفت هذي الخطوه
if(n%2==0){
n++;
}
اذا كان n زوجي بتضيف عليه 1 اما اذا فردي ما يسوي شي؟!
انا على حسب فهمي للسؤال يقول ابيك تطبع المجموع والمعدل من n to m
منتظر ردك ...
اتفق معاك اخوي ,,, من مراجعتي للكود اتضح انه يقوم بجعل اول رقم فردي , ثم يقوم بعملية الجمع اذا يكون التعديل للكود كالتالي
#include <iostream.h>
#include <stdio.h>
int main()
{
int n, m , sum=0 ,average, count=0;
cin>>n>>m;
while(m>n)
{
sum=sum+n;
count++;
n++;
}
average=sum/count;
cout<<" the sum is ="<<sum<<"the average is ="<<average;
getchar();
return 0;
}
وبالنسبة للسؤال الخامس
5) Input in the number of seconds , output the number of hours ,minutes , seconds.
Example: 4000 second seconds=1hors+6mintes + 40 seconds.
جرب تدخل 3600
المفروض الناتج بيكون 1 ساعه
لاكن هنا الناتج يقول 1 ساعه و6 دقايق !!!!!
انا حليته بطريق ثانيه بس فيها مشكله اذا ادخلت 3600 الناتج يكون 60 دقيقه المفروض يقول 1 ساعه
#include <iostream.h>
#include <stdio.h>
int main()
{
double x,y;
int h,m,s;
cout<<"enter number of seconds ?\n";
cin>>x;
y=x;
h=x/3600;
x=x/3600;
x=x-h;
m=x*60;
x=x*60;
x=x-m;
s=x*60;
cout<<y<<" seconds = "<<h<<" hours "<<m<<" mintes "<<s<<" seconds";
getchar();
return 0;
}
ممكن يكون الحل طويل لاكن هذا الحل اللي طرا على باللي اول ما قريت السؤال
اما انو يكتب 60 دقيقه بدل ساعه ولا يضيف 6 دقايق من عنده!!!!!!!!!!
وانا الحين الحل التمارين الباقي اذا فيها شي اقولك
تحياتي
بالنسبه لهذا السؤال انا حليت بهذي الفكره ,,,
مثال يكون المدخل 5555
cin>>input;
h=input/3600;عدد الساعات = عدد الثواني تقسيم ( 60*60) اذا يكون عدد الساعات 1.543 ولكن سوف يتم تخزين عدد صحيح 1 فقط في المتغير وهو الواحد ويتجاهل الاعداد الواقعه بعد الفاصله العشريه .
m=input%3600/60;عدد الدقائق = الباقيمن قسمة عدد الثواني على3600 وهو(1955) تقسيم 60 ... فيصبح الناتج =32.5833 طبعا بياخذ المتغير قيمة عدد صحيح 32 دقيقه
s=input%60; عدد الثواني المتبقيه = الباقي من قسمة عدد الثواني على 60 =35 ثانيه
جرب ادخل 3600 النتيجه ساعه واحده فقط ...
اخوي انا عدلت على هذا الكود في الاعلى ...
اشكرك اخوي ( mm-170) جزيل الشكر و أنا في انتظار رأيك ... في باقي الاكواد
كونت والله انك كفو الله يعطيك العافية وما قصرت:):):)
الكونت
يعطيك العافيه يابطل على مشاركتك لنا معلوماتك
بس لي ملاحظه بسيطه بخصوص حل السؤال السادس واللي استخدمت فيه break وهو الشي اللي ذكر الدكتور عبدالرحمن انه محظور عموما ً يعطيك الف عافيه مره ثانيه على الحلول
كونت والله انك كفو الله يعطيك العافية وما قصرت:):):)
الله يعافيك اخوي ودود ,
وتشكر على مرورك ..
الكونت
يعطيك العافيه يابطل على مشاركتك لنا معلوماتك
بس لي ملاحظه بسيطه بخصوص حل السؤال السادس واللي استخدمت فيه break وهو الشي اللي ذكر الدكتور عبدالرحمن انه محظور عموما ً يعطيك الف عافيه مره ثانيه على الحلول
معاك حق اخوي فراس احنا مادرسنا switch عشان كذا مانعرف شي اسمه break ولكن هذا مايمنع انو نتعلمها ......
وتشكر على مرورك فراس .........
طالب بكالوريس مكروف
01-06-09, 09:29 PM
مشكوووووور وربي يعاافيك
جريح الايام
02-09-09, 04:22 PM
الله يعطيكم العافيه جميعا
الاختبار بكره الله يعديه على خير
المشاكس
02-23-09, 05:12 AM
والله اني حامل الماده عند خالد الراجح حسسسسبي الله عليه
vBulletin® v3.8.4, Copyright ©2000-2012, TranZ by world 4arab