lundi 28 septembre 2015

Escape if statement in C

#pragma warning(disable: 4996)
#include<stdio.h>
int main(){    
    double array1[8] = {0};    
    double data;    
    double lg_data=0;    
    int i = 0, j = 0;    
    int count = 1;    
    int lg_count = 1;    

    printf("7개의 양의 실수를 입력하세요.(마지막 원소는 자동으로 -1이 됩니다.)\n");
    for (i = 0; i < 8; i++){
        if (i == 7){
            array1[i] = -1;
        }
        else{
            scanf("%lf", &array1[i]);
        }
    }//end of loop
    for (i = 0; i < 8; i++){
        count = 1;
        for (j = 0; j < i; j++){
            if (array1[i] == array1[j]){
                count++;
                if (count >= lg_count){
                    lg_count = count;
                    data = array1[i];
                    if (data >= lg_data){
                        lg_data = data;
                    }
                }
            }
            else{
                if (array1[i] >= lg_data){
                    lg_data = array1[i];
                    lg_count = count;
                }
            }
        }//end of for(j)
    }//end of for(i)
    printf("%lf %d\n", lg_data, lg_count);
    return;

If there are values that agree with if (array1[i] == array1[j]) statement, I want to stop for loop even though still have values that agree with else statement. I already tried to put break statement inside if statement ... but it didn't work. How could I solve this problem?

Aucun commentaire:

Enregistrer un commentaire