I know this may seem simple but i cant seem to remeber how to solve this type of problems.
Write a program to read a positive integer N and find the average of all even numbers between 1 and N inclusive. Perform any necessary validation.
and
Question #2
The data for a program are as follows: [20]
The first value is a number which represents the original amount in a bank account. The next sets of values are given in pairs. The first value of a pair is a transaction code which can be 1 or 2 and the second value is a positive number representing an amount of money.
A code of 1 means that the amount following the code is to be withdrawn from the account.
A code of 2 means that the amount following is to be deposited to the account.
The end of the data is indicated by a line containing a “code” of 0 only. Some sample data are:
3000.00
1 500.00
1 300.00
2 1500.00
1 400.00
2 700.00
0
Write a program to read any set of data given in the above format and print
• The total amount deposited to the account
• The total amount withdrawn from the account
• The final amount in the account
HELP!!!!! with C programming
Started by russian777, Oct 06 2008 02:47 PM
12 replies to this topic
#1
Posted 06 October 2008 - 02:47 PM
|
|
|
#2
Posted 06 October 2008 - 04:28 PM
What have you written so far? Do you at least know how to use arrays, printf and scanf?
#3
Posted 06 October 2008 - 04:40 PM
yeah i know basic functions of C , just started using arrays. But you woudn't need it for the first one?
#4
Posted 06 October 2008 - 05:22 PM
No. The average of SUM(1..N) is (1 + N) / 2. At least try that one.
#5
Posted 06 October 2008 - 05:24 PM
aight look on the first one , it compiles no problem , but after i enter a value for N it jus pauses and ntn happens, it just there....
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum=0,count=0,n=-1,i,total;
float avg;
printf("enter a positive value for n");
scanf("%i",&n);
while(n>0){
for(i=2;i<n;i=i+2){
sum=sum + i;
total=total + 1;
}
}
avg=(float) sum / total;
printf("average is %i",avg);
system ("pause");
return 0;
}
Edited by WingedPanther, 07 October 2008 - 08:15 AM.
add code tags
#6
Posted 06 October 2008 - 05:26 PM
For the first one, divide n by 2 and add one.
For the second, fgets() each line, then check the first character. If it is '0' end the program, if it is 1 or two, get the value using sscanf(), and perform the action required.
For the second, fgets() each line, then check the first character. If it is '0' end the program, if it is 1 or two, get the value using sscanf(), and perform the action required.
#7
Posted 06 October 2008 - 05:48 PM
Use [I]
russian777, you're responsible for putting this in a loop. Try writing pseudocode for the second problem.
[/I] tags, please. It's hard to read. Aereshaa, it's (N + 1)/2, [I]not[/I] N/2 + 1.
[CODE]
#include <stdio.h>
int main(void)
{
int n;
//input number
scanf("%d",&n);
//perform validation here...
//now print out the result.
printf("Average: %f", (n + 1) / 2.0); //implicit conversion; notice the "2.0"
//do some other stuff...
//exit
return 0;
}
russian777, you're responsible for putting this in a loop. Try writing pseudocode for the second problem.
#8
Posted 06 October 2008 - 05:54 PM
Nope:
n =
3 -> 2
4 -> 3
5 -> 3
6 -> 4
7 -> 4
under yours:
3 -> 2
4 -> 2 //WRONG!!!
See? In all cases it's n/2 + 1! Unless you don't know integer division cuts off the remainder, that should be self-evident:
n =
3 -> 2
4 -> 3
5 -> 3
6 -> 4
7 -> 4
under yours:
3 -> 2
4 -> 2 //WRONG!!!
See? In all cases it's n/2 + 1! Unless you don't know integer division cuts off the remainder, that should be self-evident:
avg_evens_1toN(int n){
return n/2 + 1;
}
EDIT: Actually, I'm not sure you read the question right: it's the average of all even number from 1 to N!
#9
Posted 06 October 2008 - 06:07 PM
OH! My bad, I didn't see the "even" part. Well, that's embarassing...
#10
Posted 06 October 2008 - 06:07 PM
Aight i get what you and aereshaa are saying. But heres it in code
The psuedocode for the second one :
withdrawn = 0
deposited = 0
print "enter original amount"
read orig
print "enter 1 or 2, 1 for withdrawing, 2 for depositing, 0 terminates program."
read code , amount
While (code != 0) DO
If code = 1 THEN
while orig > amount DO
final = orig - amount
withdrawn = amount
End While
End If
If code = 2 THEN
final = orig + amount
deposited = amount
End If
End While
Print "final amount is : ",final
Print "amount withdrawn is:",withdrawn
Print "amount deposited is:",deposited
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum=0,count=0,n=-1,i,total;
float avg;
printf("enter a positive value for n\n");
scanf("%i",&n);
while(n>0){
for(i=2;i<n;i+=2){
sum=sum + i;
total=total + 1;
}
}
avg= (float)sum / total;
printf("average is %i",avg);
system ("pause");
return 0;
}
The psuedocode for the second one :
withdrawn = 0
deposited = 0
print "enter original amount"
read orig
print "enter 1 or 2, 1 for withdrawing, 2 for depositing, 0 terminates program."
read code , amount
While (code != 0) DO
If code = 1 THEN
while orig > amount DO
final = orig - amount
withdrawn = amount
End While
End If
If code = 2 THEN
final = orig + amount
deposited = amount
End If
End While
Print "final amount is : ",final
Print "amount withdrawn is:",withdrawn
Print "amount deposited is:",deposited
#11
Posted 06 October 2008 - 06:09 PM
so what do i do then , i jus paste what aereshaa said where my print average statement is ?
#12
Posted 06 October 2008 - 06:14 PM
Or figure it out yourself. Your pseudocode looks good for #2, try implementing it.


Sign In
Create Account


Back to top









