PDA

View Full Version : C++ beginner need help



Java
September 6, 2008, 12:41 PM
I need to write a program to find the sum and average of two numbers in C program the one that starts with header files like #include<stdio.h> and #include<conio.h> up till now i cannot write a program that works keep in mind i am a beginner

crosswire
September 6, 2008, 02:40 PM
I need to write a program to find the sum and average of two numbers in C program the one that starts with header files like #include<stdio.h> and #include<conio.h> up till now i cannot write a program that works keep in mind i am a beginner

A program can be seen as a step by step appoach to do work

First user inputs numbers that are variables or data that must be stored in memory

google scanf

Next basic maths calculate the average from those variable

google "assigning variables"

Finally the result (another variable stored in memory) is displayed on the screen

google printf

Post your links here and I can explain them.

You should have a basic ability to use a C compiler. Practice compiling a "Hello World" project befor you start

Utech22
September 6, 2008, 03:35 PM
Longtime I dont do C/C++


#include<stdio.h>
#include<conio.h>

float x = 5, y = 7, avg, sum;

void main()
{
sum = x + y;
avg = sum / 2;

cout<< "The results are: \n ";
cout<< sum;
cout<< avg;
}
You can accept the input from the user using scanf

malco1987
September 6, 2008, 04:05 PM
OK let me try



//
// Java you can change the title how you see fit but i will use this one
// Program to calculate the average of two numbers
// Average = sum / 2
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
// enter First Number
int numbera;
cout << "Enter First Number:";
cin >> numbera;

// enter Second Number
int numberb;
cout << "Enter Second Number:";
cin >> numberb;

// use formula to calculate sum of numbera and numberb
int sum;
sum = numbera + numberb;

// use formula to calculate average of numbera and numberb
int average;
average = sum / 2

// output the results (followed by a NewLine)
cout << "The Average Of The Numbers Are:";
cout << average << endl;

// wait until user is ready before terminating program
system("PAUSE")
return 0;
}


That should do it i haven't tried compiling it as yet but if you are using the right compiler you should be able to see any errors so edit as you wish

AND KEEP IN MIND THAT I DID THIS OFF THE TOP OF MY HEAD so it will not be perfect

PS copy it into your compiler make sure nothing is in the same line as the { well u should know why

Utech22
September 6, 2008, 04:16 PM
That looks complicated for a noob.

malco1987
September 6, 2008, 04:27 PM
i actually thought yours was more difficult thats why i posted mine

PS: Java if you told me that any one of the variables would have a decimal then i would have to change the code

Mixmasterxp
September 6, 2008, 09:36 PM
a so unno just write out the man/woman homework star. a just straight copy and paste and loose marks that ya now.

Why did you need to use conio?

malco1987
September 8, 2008, 09:45 AM
No actually if he is true to himself he would never just copy and paste he would try to understand it fully first

Plus i posted it for him to make it his own, so to speak

Billerg
September 8, 2008, 09:57 AM
I need to write a program to find the sum and average of two numbers in C program the one that starts with header files like #include<stdio.h> and #include<conio.h> up till now i cannot write a program that works keep in mind i am a beginner
the best place to start is by posting what you already did. Makes no sense us trying to teach you individual coding styles when you will need to develop your own.

malco1987
September 8, 2008, 03:23 PM
So java you not even come drop a link or nothing, let us know what's going on

Java
September 25, 2008, 04:58 PM
include <stdio.h>
include<conio.h>
void main ()
}
int num1,num2,sum;
sum=num1+num2;
printf("enter the first number/n");
scanf("%d",&num1);
printf("enter second number/n");
scanf("%d",&num2);
printf("the sum is, sum");
getch();
}

Utech22
September 26, 2008, 12:03 AM
I dont have a s/w to test it but:

printf("the sum is, sum"); should be
printf("the sum is %d", sum);

I'll have to check it out still. do you see the error above?

recursion
September 26, 2008, 04:07 AM
I dont have a s/w to test it but:


printf("the sum is, sum"); should be
printf("the sum is %d", sum);

I'll have to check it out still. do you see the error above?

Fixed .

dujucell
March 12, 2009, 09:06 AM
1st of all u r mixin c++ with c libraries which is wrong, if u r using c u use stdio.h if u use c++ u use iostream. This is C code below

#include <stdio.h>
#include <conio.h>

void main (void)
{
int n = 0, m = 0;
float total = 0.00;
scanf("%d", &m);
scanf("%d", &n);
total = n+m;
printf("%d",total/2);
getch();
}