i can't seem to find the right algorithm...

0 pedro gutierrez · November 26, 2014
Here's a program i'm trying to make.
The idea is to evaluate if a bank transaction is suspicious or not.
For that i'll have to make a program that receives different transactions (different numbers) and if any of those transaction is greater that the average (of absolute values) times a number x then the account would be classified as suspicious.
The input will be a txt document in which i would have a real number (that needs to be possitive) and then a list of integers ending with the number 0 and then I would have to print a few things in a certain way (you can understand that from the code i wrote...). Here's the code:

#include <stdio.h>
#include <stdlib.h>

int main()
    double x;
    printf("Enter a positive number:\n");
    scanf("%lf", &x);
    while (x<=0){
        printf("Invalid number. Please enter again:\n");
        scanf("%lf", &x);
    int i, y, balance=0;
    double sum = 0, average;
    for (i=0; y !=0; i++){
        scanf("%d", &y);
        balance += y;
        if (y<0){
            y = -y;
        sum +=y;
    average =sum/(i-1);
    printf("Account balance: %d \n", balance);
    printf("Average amount: %.2f \n", average);
 if ((y/average) > x) {
        printf("Suspicious account!\n");
    else {
        printf("No suspicious activity detected.\n");

    return 0;

My problem arises at the if statement at the end of the code.
I'm supposed to evaluate that inside the for loop because I need every value of y but I also need the value of "average" which I can only get to it after the for loop because i need to reach the last possible value for "y", so it looks that i'm in a bit of a problem...

Thanks in advance for your help :-)

Post a Reply


Oldest  Newest  Rating
0 pedro gutierrez · November 27, 2014
I'm using redirection to read the file of input, and I can't know in advance what the average will be. 

I mean, I need to caclulate the average and only then use it in the code.

I'm talking about a homework I need to do and I cana only use the input data once, just as it is written in the txt file (a line of a few numbers ending in a 0 to end the search).

Do you have any other idea?
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator