Άσκηση Να γράψετε πρόγραμμα που θα διαβάζει δύο μονοδιάστατους πίνακες Α και Β 10 θέσεων και θα δημιουργεί το μονοδιάστατο πίνακα C 20 θέσεων για τον οποίο ισχύει C[i]=A[i] C[i]=B[i-10] αν i=1,,10, και αν i=11,,20. Το πρόγραμμα θα τυπώνει και τους τρεις πίνακες.
#include <iostream> using namespace std; int main() float a[10], b[10], c[20]; int i; char ask; do cout<<"\nassign values to the array A : ";
for (i=0; i<10; i++) cout<<"\nassign value to element"<<i+1 <<" for A: "; cin>>a[i]; cout<<"\nassign values to the array B: "; f or (i=0; i<10; i++) cout<<"\nassign value to element "<<i+1 <<" for B: "; cin>>b[i];
cout<<"\narray A is:"; for (i=0; i<10; i++) cout<<" "<<a[i]; cout<<"\narray B is:"; for (i=0; i<10; i++) cout<<" "<<b[i]; cout<<"\narray C is:"; for (i=0; i<20; i++) if (i<10) c[i] = a[i];
else c[i] = b[i-10]; cout<<" "<<c[i]; cin>>ask; while (ask=='y' ask=='y'); return 0;
Άσκηση Να γράψετε πρόγραμμα που θα διαβάζει ένα μονοδιάστατο πίνακα Α 10 θέσεων και θα δημιουργεί το μονοδιάστατο πίνακα Β 11 θέσεων για τον οποίο ισχύει Β[i]=A[i] αν i=1,,10, και στη θέση Β[11] θα έχει το μέσο όρο των στοιχείων Β[1] έως Β[10]. Το πρόγραμμα θα τυπώνει και τους δύο πίνακες.
#include <iostream> using namespace std; int main() float a[10], b[11], sum = 0, avg; int i; cout<<"\nassign values to the array A: ";
for (i=0; i<10; i++) cout<<"\nassign value to element "<<i+1 <<" of array A: "; cin>>a[i]; b[i] = a[i]; sum += a[i]; avg = sum/10; b[10] = avg;
cout<<"\narray A is: "<<endl; for (i=0; i<10; i++) cout<<" "<<a[i]; cout<<"\narray B is: "<<endl; for (i=0; i<11; i++) cout<<" "<<b[i]; return 0;
Άσκηση Να γράψετε πρόγραμμα που θα διαβάζει ένα μονοδιάστατο πίνακα Α 10 θέσεων και θα δημιουργεί το μονοδιάστατο πίνακα Β 10 θέσεων για τον οποίο ισχύει B[ i] A[ j] 1 j i για κάθε i=1,,10. Το πρόγραμμα θα εκτυπώνει και τους δύο πίνακες.
#include <iostream> using namespace std; int main() float a[10], b[10]; int i,j; cout<<"\nassign values to the array A: ";
for (i=0; i<10; i++) cout<<"\nassign value to element "<<i+1 <<" of array A: "; cin >> a[i]; for(i=0;i<10;i++) b[i]=0; for(j=0;j<=i;j++) b[i]=b[i]+a[j];
cout << "\narray A is: "; for (i=0; i<10; i++) cout << a[i] << " "; cout << "\narray B is: "; for (i=0; i<10; i++) cout << b[i] << " "; return 0;
Άσκηση Να γράψετε πρόγραμμα που ζητάει από το χρήστη ένα θετικό ακέραιο (απαιτείται έλεγχος ορθότητας δεδομένων) και θα εκτυπώνει όλους τους διαιρέτες του.
#include <iostream> //The factors of a positive integer using namespace std; int main() long int n,i; do cout<<"assign a positive value >1 to n:"; cin>>n; while(n<=0); cout<<"the factors of "<<n<<" are: "<<endl;
for(i=1;i<=n;i++) if(n%i==0) cout<<i<<" "; return 0;
Άσκηση Να γράψετε πρόγραμμα που ζητάει από το χρήστη ένα θετικό ακέραιο μεγαλύτερο του 1 (απαιτείται έλεγχος ορθότητας δεδομένων) και θα εκτυπώνει την ανάλυσή του σε πρώτους αριθμούς. Οι πρώτοι παράγοντες θα τοποθετούνται σε πίνακα, με χρήση του οποίου θα γίνεται και η εκτύπωση. Επίσης αν ο δοσμένος αριθμός είναι πρώτος, το πρόγραμμα θα εκτυπώνει ένα σχετικό μήνυμα.
#include <iostream> //Analysis of a number into prime factors using namespace std; int main() long int factors[10000], n,m,a,i,j; do cout<<"assign a positive value >1 to n:"; cin>>n; a=n; while(n<=1);
i=0; m=2; while(n>1) while(n%m==0) factors[i]=m; i++; n=n/m; m++;
cout<<"the analysis of the number "<<a <<" in prime factors is:"<<endl; for(j=0;j<i;j++) cout<<factors[j]<<" "; if (i==1) cout<<"\nthe number "<<a <<" is prime"; return 0;
Άσκηση Να γράψετε πρόγραμμα που θα ζητάει από το χρήστη ένα θετικό ακέραιο n (απαιτείται έλεγχος ορθότητας δεδομένων) και θα εκτυπώνει το πλήθος των ακεραίων που είναι σχετικώς πρώτοι με το δοσμένο αριθμό n (συνάρτηση φ του Euler). (Αν n>1, τότε οι σχετικώς πρώτοι αριθμοί με τον n, είναι οι μικρότεροι (από τον n) θετικοί ακέραιοι, οι οποίοι είναι πρώτοι με τον n). Αν r 1,r 2,,r m είναι οι διακεκριμένοι πρώτοι παράγοντες του n, τότε αποδεικνύεται ότι ( n) n(1 1/ r )...(1 1/ r ) 1 m
#include <iostream> //The Euler's totient funtion using namespace std; int main() long int n,m,a,i,j; float factors[10000], p; do cout<<"assign a positive value >1 to n:"; cin>>n; a=n; while(n<=1);
i=0; m=2; while(n>1) while(n%m==0) factors[i]=m; i++; n=n/m; m++;
cout<<"the analysis of the number "<<a <<" in prime factors is: "<<endl; for(j=0;j<i;j++) cout<<factors[j]<<" "; cout<<endl<<endl<<"the function of Euler, outputs for "<<a<<", the number: "<<endl; p=a*(1-1/factors[0]); for(j=1;j<i;j++) if(factors[j]!=factors[j-1]) p=p*(1-1/factors[j]); cout<<p; return 0;
Άσκηση Να γράψετε πρόγραμμα που θα ζητάει από το χρήστη δύο σύνολα Α και Β ακεραίων αριθμών, δέκα στοιχείων το καθένα. Το πρόγραμμα θα εμφανίζει τα σύνολα στην οθόνη και ένα μήνυμα για το αν τα σύνολα είναι ίσα ή όχι.
#include<iostream> using namespace std; int main() int i,j,k,a[10],b[10];
cout<<"\ngive the elements (pairwise disjoint) of the set A"; for (i=0;i<10;i++) cout<<"\ninsert the "<<i+1<<" number: "; cin>>a[i]; do k=0; for (j=0;j<i;j++) if(a[i]==a[j]) k=1; cout<<"\nyou have already given the number "<<a[i]; cout<<"\ninsert a new number: "; cin>>a[i]; while(k==1);
cout<<"\ngive the elements (pairwise disjoint) of the set B"; for (i=0;i<10;i++) cout<<"\ninsert the "<<i+1<<" number: "; cin>>b[i]; do k=0; for (j=0;j<i;j++) if(b[i]==b[j]) k=1; cout<<"\nyou have already given the number "<<b[i]; cout<<"\ninsert a new number: "; cin>>b[i]; while(k==1);
//Check A and B for equality k=0; for (i=0;i<10;i++) for(j=0;j<10;j++) if (a[i]==b[j]) k++;
cout<<"\nthe set A = "; for (i=0;i<9;i++) cout<<a[i]<<", "; cout<<a[9]<<""; cout<<"\n\n\nthe set B = "; for (i=0;i<9;i++) cout<<b[i]<<", "; cout<<b[9]<<""; if (k==10) cout<<"\n\n\nthe sets A and B are equal"; else cout<<"\n\nthe sets A and B are not equal"; return 0;