Pouvez vous m'aider ! Please

Pouvez vous m'aider ! Please - C - Programmation

Marsh Posté le 16-11-2011 à 23:41:48    

bonjour a tous, c'est ma première année informatique et j'ai des exercices concernant language c :
 
Ex01:
Programme calculant la moyenne de 3 notes données d’un
étudiant.
 
Ex02:
Programme calculant le reste et le quotient de la division
entière de 2 entiers donnés.

Ex03:

Programme résolvant l’équation Ax2+Bx+C=0.
 
Ex04:
Programme résolvant une équation du 1er degré : Ax+B=0
 
Donc svp Je n'oublierai pas votre aide

Reply

Marsh Posté le 16-11-2011 à 23:41:48   

Reply

Marsh Posté le 17-11-2011 à 07:46:30    

any one !! i really need this

Reply

Marsh Posté le 17-11-2011 à 10:13:10    

Why no one is answering !!

Reply

Marsh Posté le 17-11-2011 à 11:06:40    

Because you didn't show up any work done for this assignment.
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 19-11-2011 à 12:49:47    

Hi again, sorry for that you have a point so this is my solution for this programs so please correct if am wrong  
Ex01:
#include<stdio.h>
#include<conio.h>
main() {
float n1,n2,n3,moyenne;
cout << "donner les trois note" ;
cin >> n1 >> n2 >> n3 ;
moyenne = (n1+n2+n3)/3 ;
cout << "moyenne = " << moyenne ;
system ("pause" );
}
 

Reply

Marsh Posté le 19-11-2011 à 15:45:54    

Code :
  1. #include <stdio.h>
  2. #include <conio.h> // That works only with the old Borland C or C++
  3. main() {  // no! int main ...
  4. float n1,n2,n3,moyenne;
  5. cout << "donner les trois note" ;  // this is C++, not C
  6. cin >> n1 >> n2 >> n3 ;
  7. moyenne = (n1+n2+n3)/3 ;
  8. cout << "moyenne = " << moyenne ;  // this is C++, not C
  9. system ("pause" );  // you need to return something in C
  10. }


 
That was not too bad, but you mixed C and C++
 
A first version in C:
 

Code :
  1. #include <stdio.h>
  2. int main() {
  3.     float n1, n2, n3, moyenne;
  4.     printf("Donnez les trois notes: " );
  5.     scanf("%f %f %f", &n1, &n2, &n3);
  6.     moyenne = (n1+n2+n3)/3 ;
  7.     printf("La moyenne est: %f\n", moyenne);
  8.     system ("pause" );
  9.     return 0;
  10. }


 
This will work, but only if the input is correct.
In C, you must check the validity of external data.
The external data comes from the scanf function, so you need to check its manuel page  
in order to know how this function signals that something bad occurred with it.

Citation :

Return Value
 
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
 
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.


So now you can have a user-proof version:

Code :
  1. #include <stdio.h>
  2. int main() {
  3.     float n1,n2,n3,moyenne;
  4.     int lus;
  5.     printf("Donnez les trois notes: " );
  6.     lus = scanf("%f %f %f", &n1, &n2, &n3);
  7.     if (lus != 3) {
  8. printf("Valeurs incorrectes en entree\n" );
  9. system ("pause" );
  10. return 1;
  11.     }
  12.     moyenne = (n1+n2+n3)/3 ;
  13.     printf("La moyenne est: %f\n", moyenne);
  14.     system ("pause" );
  15.     return 0;
  16. }


 
You can enhance it with a better version, conformant to standards for the return value:

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4.     float n1,n2,n3,moyenne;
  5.     int lus;
  6.     printf("Donnez les trois notes: " );
  7.     lus = scanf("%f %f %f", &n1, &n2, &n3);
  8.     if (lus != 3) {
  9. printf("Valeurs incorrectes en entree\n" );
  10. system ("pause" );
  11. return EXIT_FAILURE;
  12.     }
  13.     moyenne = (n1+n2+n3)/3 ;
  14.     printf("La moyenne est: %f\n", moyenne);
  15.     system ("pause" );
  16.     return EXIT_SUCCESS;
  17. }


and then be smarter for the error message:

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4.     float n1,n2,n3,moyenne;
  5.     int lus;
  6.     printf("Donnez les trois notes: " );
  7.     lus = scanf("%f %f %f", &n1, &n2, &n3);
  8.     if (lus != 3) {
  9. switch (lus) {
  10. case 1: // un seul nombre lu
  11.     printf("Seconde valeur incorrecte en entree\n" );
  12.     break;
  13. case 2: // deux nombres lus seulement
  14.     printf("Troisieme valeur incorrecte en entree\n" );
  15.     break;
  16. default: // aucun nombre lu
  17.     printf("Premiere valeur incorrecte en entree\n" );
  18.     break;
  19. }
  20. system ("pause" );
  21. return EXIT_FAILURE;
  22.     }
  23.     moyenne = (n1+n2+n3)/3 ;
  24.     printf("La moyenne est: %f\n", moyenne);
  25.     system ("pause" );
  26.     return EXIT_SUCCESS;
  27. }


 
If you want the moyenne with only two digits after the decimal, just replace
printf("La moyenne est: %f\n", moyenne);
with
printf("La moyenne est: %.2f\n", moyenne);
 
You can then try to enhance this exercise:
Read a fixed number of notes (here it is 3, make it work for any other number) and use an array to store the notes and use a loop to read the input.
Read a variable (but limited by a maximum number) number of notes
 
A C++ program is very different.
for example, a very minimal program (similar to the first one I wrote, with no error checking, etc) woud be:

Code :
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. int main () {
  6.     float note, moyenne = 0.0f;
  7.     cout << "Donnez les trois notes: ";
  8.     for (int i = 1; i <= 3; i++) {
  9.         cin >> note;
  10.         moyenne += note;
  11.     }
  12.     moyenne /= 3;
  13.     cout << "La moyenne est: " << moyenne << endl;
  14.     return 0;
  15. }


Compare with the equivalent C program:

Code :
  1. #include <stdio.h>
  2. int main() {
  3.     float note, moyenne = 0.0f;
  4.     int i;
  5.     printf("Donnez les trois notes: " );
  6.     for (i = 1; i <= 3; i++) {
  7.         scanf("%f", &note);
  8.         moyenne += note;
  9.     }
  10.     moyenne /= 3;
  11.     printf("La moyenne est: %f\n", moyenne);
  12.     return 0;
  13. }


 
A+,


Message édité par gilou le 19-11-2011 à 15:51:26

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 21-11-2011 à 19:07:52    

Wow! Thank You That Was Amazing, it was the best explanation i ever seen, Honestly Wow!!  

Reply

Marsh Posté le 22-11-2011 à 20:56:13    

Hi Again, more questions ^^  
we solved EX02 but i found it complicated can you help me please  

Reply

Marsh Posté le 22-11-2011 à 21:20:25    

Reply

Marsh Posté le 22-11-2011 à 22:34:41    

It is just because you don't understant the fundamental definition of a multiplication.
 
A multiplication is just the repetition of an addition:
 
a + a + ... + a, n times, is n*a. This is how you define a multiplication, the real meaning of a multiplication: repeating an addition.
 
So now, lets have to numbers, a and b, and suppose that a <= b.
 
as a is less than b, lets try to remove it as much as we can from b (we do the reverse of a multiplication: we repeat a substraction).
 
we do b -a, if this is positive, we do b - a - a, etc
at some point, we will have b - a - a - ... - a >= 0 and if we substract one times more a, we will have a negative number:
b - a - a - ... - a >= 0 and  
b - a - a - ... - a -a < 0
this is the same as  
b - (a + a + ... + a) >= 0 and
b - (a + a + ... + a) -a < 0
 
lets count the number of times that we have a in (a + a + ... + a) an name q that count.
By definition of what is a multiplication, this (a + a + ... + a) is a*q
So we have  
b - a*q >= 0 and  
b - a*q -a < 0 wich is the same as b - a*q < a
lets name r the number b - a*q
we have  
r >= 0 and
r < a
which can be written on a unique line as
0 <= r < a
 
We have b = b - a*q + a*q = r + a*q and 0 <= q < a. q is called the quotient and r the remainder of the division of b by a.
We have seen that by substracting a as much as we can, and counting the number of times we substracted, we can get the quotient and the remainder:
 
Algorithm:
[quotient = 0, remainder = b]  (initial conditions)
loop
    if remainder -a < 0  (we cannot substract a one time more) exit loop
    (else)
    remainder = remainder - a (we substract a one more time)
    quotient = quotient + 1    (we increase the number of times that we have substracted a)
end loop
 
when we exit the loop, we have the quotient (the biggest number of times that we can substract a and remain positive) and the remainder (what remains when we cannot substract a one time more).
Note: The test if remainder -a < 0 is the same as the test if remainder < a , which simpler to write.
 
This is straightforward in C:
 

Code :
  1. int quotient = 0;
  2. int remainder = b;
  3. while (remainder >= a) {
  4. remainder -= a;
  5. ++quotient;
  6. }


which is just what is done in your exercise  (there is an obvious first part, where the iSaisi1 and iSaisi2 are associated with a and b in order to have a <= b)
 
A+,


Message édité par gilou le 22-11-2011 à 22:48:58

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 22-11-2011 à 22:34:41   

Reply

Marsh Posté le 29-11-2011 à 20:05:34    

Hi Again,
THank You for your help Teacher ^^
but this time i really don't know even how to start this one so please please help me here it is :
http://www.iimmgg.com/image/32fbda [...] 28236c714c
   
 
 
 
Number Five please i've done the last ones i m gonna upload them later but now please help me with this one i promise you it will be the last time if i'm bothering You

Reply

Marsh Posté le 29-11-2011 à 22:43:59    

So the number 5 exercise asks what? to calculate y such that when x ...
So you are going to implement a function f, which, when you give a value x, returns with a value y that is calculated (from x) according to the formulas given.
So as you are going to write a function, lets name it: function5.
In input: x, a number. It is a real number, so in C, we will use the float type.
In output, y, a number. It is a real number, so in C, we will use the float type.
So we have the prototype of function5:
float function5(float x);
 
and we will use it in main as:
...
float x, y;
 
printf("Donnez la valeur de x: " );
lus = scanf("%f", &x);
.....
y = function5(x);
printf("La valeur de y est: %f, y);
...
 
So now, you have to implement function5, ie write its code:
float function5(float x) {
float y;  // we will put the calculation in it and return it
....
return y;
}
 
function5 is such that  "y is x + 8 if x <= 10"
so we have a test:  if x <= 10 , which gives in C: if (x <= 10)
and if the test is true, y is x + 8, which gives in C: y = x+8;
so combining both part, we get:
if (x <= 10) {
    y = x+8;
}
and if x is not less or equal than 10, we have something else, so we can write
if (x <= 10) {
    y = x+8;
}
else {
....
}
 
so we have the beginning of the function:
 
float function5(float x) {
  float y;  // we will put the calculation in it and return it
 
  if (x <= 10) {
      y = x+8;
  }
  else {
    ....
  }
  return y;
}
 
I hope this helps
 
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 30-11-2011 à 07:57:36    


Hello
 
Be carefull, main() isn't "void" because is "int"...


Message édité par Sve@r le 30-11-2011 à 07:57:59
Reply

Marsh Posté le 04-12-2011 à 22:59:21    

Hello sir Thank you for every thing u have done for me God bless you  
XD one last question Sir  
programme qui calcul x power n with out using (* , ^)  
no multiplication no power !!
How !!? if u can en PACAL et C++  
Thank You and sorry .

Reply

Marsh Posté le 05-12-2011 à 02:25:43    

I suppose that x and n are two positive integer (you can enhance it in the case where x is negative)
 

Code :
  1. int expo(int x, int n) {
  2.     /* bad input values */
  3.     if (x < 0 || n < 0) return -1;
  4.     if ((!x) && (!n))   return -1;
  5.     /* no need for computing values */
  6.     if (!x) return 0;
  7.     if (!n) return 1;
  8.     if (n == 1) return x;
  9.     /* main case */
  10.     int i, m;
  11.     int exp = 1;
  12.     while (n--) {
  13.         m = exp;
  14.         exp = 0;
  15.         i = x;
  16.         while (i--) {
  17.             exp += m;
  18.             /* test for overflow */
  19.             if (exp < 0) return -1;
  20.         }
  21.     }
  22.     return exp;
  23. }


 
The algorithm is simple:
First, you create a function  mult(x, y) which calculates the product of x by y (we saw last time that it can be made with additions, adding y times the number x )
now lets have look:
 
mult(1, x) = 1*x = x = x^1;
mult(mult(1, x), x) = mult(x, x) = x*x = x^2;
mult(mult(mult(1, x), x), x) = mult(x*x, x) = x*x*x
..........
mult(...mult(mult(1, x), x), x) =  mult(x*...*x, x) = x*...*x*x = x^n, when we have n times a call to the function mult
 
So we can write the algorithm:
 
exp = 1
loop n times
exp = mult(exp, x)
end loop
 
This is the most efficient way to calculate it here.
 
Note:
I suspect that you are expected to use the (less efficient) recursive algorithm:
exp(x, n) = 1 if n = 0 (and x is not 0)
exp(x, n) = mult(exp(x, n-1), x) if n is not 0
 
A+,


Message édité par gilou le 05-12-2011 à 03:14:09

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 05-12-2011 à 10:45:33    

Thank You that Was amazing God bless You TeaCher ^^
Sorry for bothering You have a niCe Day Sir .

Reply

Marsh Posté le 06-12-2011 à 12:32:01    

[:rofl] il vous flatte un peu et vous faites ses exos [:rofl]

Reply

Marsh Posté le 06-12-2011 à 13:34:32    

Pour ma dernière réponse, s'il coupe-colle mon code, c'est sur que son prof va penser qu'il est de lui :D
Tu remarqueras qu'a chaque fois, c'est une longue explication pédagogique que je fais, pas un bout de code sec.
C'est bien Shipon, ton avatar?
EDIT: Ah oui, au vu du profil, c'est clair.
A+,


Message édité par gilou le 06-12-2011 à 13:40:50

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 06-12-2011 à 13:52:07    

non mais ok pour les explications, mais pour l'exo 5 il n'a rien proposé a part un vague "ouais je l'ai fait"... ^^

 

(Et oui, c'est Shipon, je sais que tu es fan de Stellvia :D)


Message édité par Tamahome le 06-12-2011 à 13:52:50
Reply

Marsh Posté le 06-12-2011 à 14:01:56    

Oui, quelque peu fan (j'ai même la version DVD JAP non sous titrée :o ). Je regretterais éternellement qu'ils aient annulé la S2.  
 
Il avait quand même l'air d'avoir fait un minimum d'efforts.
Le plus souvent, c'est dans le topic perl que je fais le boulot à la place, pédagogiquement aussi, mais surtout parce qu'il est difficile au débutant de rentrer dans ce langage (peu de bouquins d'initiation à Perl bien foutus).
A+,


Message édité par gilou le 06-12-2011 à 14:03:32

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 06-12-2011 à 14:04:43    

voui pour la s2 (/HS) :( :( :(

Reply

Marsh Posté le 12-12-2011 à 21:53:04    

don't u worry i never do that ^^ , + we have a stupid teacher im not doing this exercises to please him its for my self although its hard to much ur explanation but i try to do it my way So i really thank u about every thing Peace
   

Reply

Marsh Posté le 19-12-2011 à 22:40:33    

Cool, and do you expect us to show up for your exams ?


---------------
Les aéroports où il fait bon attendre, voila un topic qu'il est bien
Reply

Marsh Posté le 17-01-2012 à 19:56:02    

Hey All,
"Cool, and do you expect us to show up for your exams ?" I told Ya i'm not like that man i train my self not to please teachers so please understand that cuz u seem like u don't ^^ any way ..
i did some exs Hope they're correct so i leave that to you Pros ^^

Reply

Marsh Posté le 17-01-2012 à 20:11:13    

So please take a look :

  • Produit des N premiers entiers pairs à partir de 2.
Code :
  1. #include<iostream>
  2. using namespace std ;
  3. main()
  4. {
  5. int i,n,p;
  6. //Produit des N premiers entiers pairs à partir de 2.
  7. printf("Produit des N premiers entiers pairs à partir de 2.\n" );
  8. printf("Donner La valeur De N= " );
  9. scanf("%d",&n);
  10. i=2; p=1;
  11. while (i<=2*n) {
  12. p=p*i;
  13. i=i+2;}
  14. printf("Resultat :=%d\n ",p);
  15. system("pause" );
  16. }


  • Somme des N nombres entiers donnés.
Code :
  1. #include<iostream>
  2. using namespace std ;
  3. main()
  4. {
  5.       int N,i,x,S;
  6.       printf("Somme des N nombres entiers donnés.\n" );
  7.       printf("Donner N= " );
  8.       scanf("%d",&N);
  9.       printf("Dooner Les N Nombres:\n" );
  10.       S=0;
  11.       for (i=1;i<=N;i++)
  12.       scanf("%d",&x),
  13.       S=S+x ;
  14.      
  15.       printf("La Somme Est S= %d\n",S);
  16.       system("pause" );
  17.                       }


  • Programme Calculer le factoriel d’un entier donné N.
Code :
  1. #include<iostream>
  2. using namespace std ;
  3. main()
  4. {
  5.       int i,Fact,N;
  6.       printf("Programme Calculer le factoriel d’un entier donné N.\n" );
  7.       printf("Dooner N= " );
  8.       scanf("%d",&N);
  9.       if (N==0)
  10.           Fact=1;
  11.           else
  12.           {
  13.               for(i=1,Fact=1;i<=N;i++)
  14.                   Fact=Fact*i;}
  15.                   printf("Fact(%d)=%d\n",N,Fact);
  16.             system("pause" );
  17.             }


Well i blocked in this one, So please just how to start it will be great ^^:

  • Programme calculer Minimum de N nombres entiers donnés,

    Without Using tables Cuz we didn't get there yet .  
                                  Thank You  

Reply

Marsh Posté le 17-01-2012 à 23:39:41    

Hi Again,
Well Finaly i think i did it right(i guess)  :) :

  • Programme calculer Minimum de N nombres entiers donnés,  
Code :
  1. #include<iostream>
  2. using namespace std ;
  3. int main()
  4. { int i,n,nb,min;
  5.     i=1;
  6. printf("Programme calculer Minimum de N nombres entiers donnés.\n" );
  7.    cout<<"Combien de nombres voulez vous entrer ? N=  ";
  8.    cin>>n;
  9.    cout<<"Enter number ";
  10.    cin>>nb;
  11. do
  12. {
  13.  cout<<"Enter number ";
  14.  cin>>min;       
  15.  if(nb<=min)
  16.   min=nb;
  17.             i++;       
  18.                     }
  19. while (i<n);
  20.   printf("Minimum Number est :%d\n",min);
  21.  system("pause" );
  22. }


Reply

Marsh Posté le 18-01-2012 à 01:13:06    

First exercice.
 
I already told you! You must choose between a C++ program:
 

Code :
  1. #include <iostream>
  2. using namespace std;
  3. //Produit des N premiers entiers pairs à partir de 2.
  4. int main() {
  5.     int n;
  6.     unsigned long long int p = 1;
  7.     cout << "Produit des N premiers entiers pairs a partir de 2." << endl;
  8.     cout << "Donnez La valeur De N ";
  9.     cin >> n;
  10.     if (n < 0) {
  11.         cout << "Valeur incorrecte" << endl;
  12.     }
  13.     else if (n > 16) {
  14.         // 16 pour un unsigned long long int  
  15.         // 10 pour un unsigned long int  
  16.         // 06 pour un unsigned short int
  17.         cout << "Valeur trop grande" << endl;
  18.     }
  19.     else {
  20.         for (n *= 2; n > 1; n -= 2)
  21.             p *= n;
  22.         cout << "Resultat := " << p << endl;
  23.     }
  24. }


 
and a C program:
 

Code :
  1. #include <stdio.h>
  2. //Produit des N premiers entiers pairs à partir de 2.
  3. int main() {
  4.     int n;
  5.     unsigned long long int p = 1;
  6.     printf("Produit des N premiers entiers pairs a partir de 2.\n" );
  7.     printf("Donnez La valeur De N: " );
  8.     scanf("%d", &n);
  9.     if (n < 0) {
  10.         printf("Valeur incorrecte\n" );
  11.     }
  12.     else if (n > 16) {
  13.         // 16 pour un unsigned long long int  
  14.         // 10 pour un unsigned long int  
  15.         // 06 pour un unsigned short int
  16.         printf("Valeur trop grande\n" );
  17.     }
  18.     else {
  19.         for (n *= 2; n > 1; n -= 2)
  20.             p *= n;
  21.         printf("Resultat := %llu\n", p);
  22.     }
  23. }


 
But try not to mix both languages.
 

Code :
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. //Produit des N premiers entiers pairs à partir de 2.
  5. int main() {
  6.     vector<int> numbers;
  7.     vector<int>::size_type numbers_sz;
  8.     int i, min;
  9.     cout << "Programme calculer Minimum de N nombres entiers." << endl;
  10.     cout << "Entrez les nombres et terminez par un . ";
  11.     while (cin >> i)
  12.         numbers.push_back(i);
  13.     numbers_sz = numbers.size();
  14.     if (numbers_sz < 1)
  15.         cout << "La liste des valeurs est vide" << endl;
  16.     else {
  17.         min = numbers[0];
  18.         for (vector<int>::size_type j = 0; j != numbers_sz; j++)
  19.             if (min > numbers[j]) min = numbers[j];
  20.         cout << "La valeur Minimum est " << min << endl;
  21.     }
  22. }


 
A+,


Message édité par gilou le 18-01-2012 à 02:03:24

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le    

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed