Jump to content

HELP!!!!! with C programming

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
russian777

russian777

    Newbie

  • Members
  • PipPip
  • 29 posts
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

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
What have you written so far? Do you at least know how to use arrays, printf and scanf?

#3
russian777

russian777

    Newbie

  • Members
  • PipPip
  • 29 posts
yeah i know basic functions of C , just started using arrays. But you woudn't need it for the first one?

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
No. The average of SUM(1..N) is (1 + N) / 2. At least try that one.

#5
russian777

russian777

    Newbie

  • Members
  • PipPip
  • 29 posts
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
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
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.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
Use [I]
[/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
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
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:
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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
OH! My bad, I didn't see the "even" part. Well, that's embarassing...

#10
russian777

russian777

    Newbie

  • Members
  • PipPip
  • 29 posts
Aight i get what you and aereshaa are saying. But heres it in code


#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
russian777

russian777

    Newbie

  • Members
  • PipPip
  • 29 posts
so what do i do then , i jus paste what aereshaa said where my print average statement is ?

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
Or figure it out yourself. Your pseudocode looks good for #2, try implementing it.