Jump to content

How to write a program that evaluates min/max val..

- - - - -

  • Please log in to reply
5 replies to this topic

#1
JaaneKyun

JaaneKyun

    Newbie

  • Members
  • Pip
  • 5 posts
Hi everyone..

I have to create a program in which I must write a program that looks something like this:

EXAMPLES:

Enter 10 digits: 4 4 4 4 4 4 4 4 4 4

All values are equal

PROGRAM ENDS


Enter 10 digits: 9 9 9 9 9 9 9 9 8 8

Min= 8

Max= 9

Min_count= 2

Max_count= 8

Between_count= 0

PROGRAM ENDS



DIRECTIONS: Write a program which will prompt the user to enter 10 valid C int values each >= 0. Compute the minimum of the 10 values. Compute the maximum of the 10 values. If min = max, print the message "All values are equal" then print the usual PROGRAM ENDS line, and terminate the program. Otherwise, display the minimum value. Display the maximum value. Display the number of elements equal to the minimum. Display the number of elements equal to the maximum. Display the number of elements with values between min and max (i.e. display the number of elements greater than minimum and less than the maximum). Then, print the PROGRAM ENDS line and terminate the program. Ensure that your program concludes by displaying "PROGRAM ENDS" followed by a single newline character.


So far this is what I have created:

"pe4.c" 77 lines, 924 characters
     1  #include <stdio.h>

     2

     3  int main ( )

     4

     5  {

     6  int i, a[10], max;

     7  printf ("enter 10 digits ./n");

     8  for (i=0; i<10;i++){

     9  scanf ("%d",&a[i]);}

    10  max = (max, [

    11

    12  int maximum ( ){

    13

    14

    15  int max = a[0];

    16  for(i=0; i<10; i++)

    17  {

    18  if (a[i] > max)

    19  {

    20  max = a[i];

    21  }

    22  }

    23  return max;

    24  }

    25  

    26  

    27  int minimum ( )

    28  {

    29  int min = a[0];

    30  

    31  for(i=0; i<10; i++)

    32  if (a[i] < min)

    33  {

    34  min = a[i]

    35  }

    36  return min;

    37  }

    38  int max_count ()

    39  {

    40  int i, max, current_max, current, numbers;

    41  int count = 0;

    42  for (i = 0; i < numbers; i++)

    43  {

    44  scanf("%d", ¤t);

    45  if (current_max > current)

    46  {

    47  max = current_max;

    48  count = 1;

    49  if (current == current_max)

    50  count++;

    51  }

    52  }

    53  

    54  }

    55  

    56  int min_count ()

    57  {

    58  int i, min, current_min, current, numbers;

    59  int count = 0;

    60  for (i = 0; i < numbers; i++)

    61  {

    62  scanf("%d", ¤t);

    63  if (current_min < current)

    64  {

    65  min = current_min;

    66  count = 1;

    67  if (current == current_min)

    68  count++;

    69  }

    70  }

    71  }

    72  {

    73  printf ("Numbers between min and max")

    74  

    75  printf ("PROGRAM ENDS");

    76  }

    77  printf ("PROGRAM ENDS");


Can anyone please help me out with creating the number of elements with values between min and max?

Thank you!

#2
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
Maybe something like:
#include <stdio.h>


int main( int argc, char* argv[] )
{
    int values[10] = {0};
    const int count = sizeof(values) / sizeof(int);

    for ( int i = 0; i < count; ++i )
    {
        ::printf( "enter value=" );
        ::scanf( "%d", &values[i] );

        if ( 0 > values[i] )
        {
            ::printf( "value must be positive\n" );

            --i;
            continue;
        } // if
    } // for

    int max = values[0];
    int max_count = 0;

    for ( int i = 0; i < count; ++i )
    {
        if ( values[i] > max )
        {
            max = values[i];
            max_count = 1;
        } // if
        else if ( values[i] == max )
        {
            ++max_count;
        } // else if
    } // for

    int min = values[0];
    int min_count = 0;

    for ( int i = 0; i < count; ++i )
    {
        if ( values[i] < min )
        {
            min = values[i];
            min_count = 1;
        } // if
        else if ( values[i] == min )
        {
            ++min_count;
        } // else if
    } // for

    if ( min == max )
    {
        ::printf( "all values are equal\n" );
    } // if
    else
    {
        ::printf( "max value=%d\n", max );
        ::printf( "max count=%d\n", max_count );

        ::printf( "min value=%d\n", min );
        ::printf( "min count=%d\n", min_count );

        ::printf( "between count=%d\n", count - max_count - min_count );
    } // else

    ::printf( "program ends\n" );
    return 0;
}

std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

#3
JaaneKyun

JaaneKyun

    Newbie

  • Members
  • Pip
  • 5 posts
Thank you for the quick response!

I am getting a few errors when I try to run the program..

admiral% gcc pe6.c
pe6.c: In function `main':
pe6.c:9: error: 'for' loop initial declaration used outside C99 mode
pe6.c:11: error: syntax error before ':' token
pe6.c:16: error: syntax error before ':' token
pe6.c:19: error: continue statement not within a loop
pe6.c:26: error: redefinition of 'i'
pe6.c:9: error: previous definition of 'i' was here
pe6.c:26: error: 'for' loop initial declaration used outside C99 mode
pe6.c:42: error: redefinition of 'i'
pe6.c:26: error: previous definition of 'i' was here
pe6.c:42: error: 'for' loop initial declaration used outside C99 mode
pe6.c:57: error: syntax error before ':' token
pe6.c:61: error: syntax error before ':' token


Are any of these errors obvious to you? Its kind of hard to me to figure out.. but if you cant find them I understand

#4
camdaddy09

camdaddy09

    Newbie

  • Members
  • PipPip
  • 15 posts
::printf()
should be
printf()

no colons
when i got rid of the colons it compiled just fine.
[SIGPIC][/SIGPIC]

#5
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a[10];
    int i;
    int min,max;
    int min_cnt,max_cnt;

    printf("Enter 10 values\n");
    for(i=0;i<10;i++){
        scanf("%d",&a[i]);
        if(a[i]<=0){
           printf("Values only greater than 0");
           exit(1);
        }
    }

    max=min=a[0];
    min_cnt=max_cnt=1;
    for(i=1;i<10;i++){
        if(a[i]==max || a[i]==min){
           if(a[i]==max)
              max_cnt++;
           if(a[i]==min)
              min_cnt++;
        }
        else if(a[i]>max){
            max=a[i];
            max_cnt=1;
        }
        else if(a[i]<min){
            min=a[i];
            min_cnt=1;
        }
    }

    if(max==min)
        printf("All values are equal\n");
    else{
        printf("Min=%d\nMax=%d\n",min,max);
        printf("Min count=%d\nMax count=%d\n",min_cnt,max_cnt);
        printf("Between count=%d\n",10-(min_cnt+max_cnt));
    }
    system("pause");
    return 0;
}

Warrior

#6
JaaneKyun

JaaneKyun

    Newbie

  • Members
  • Pip
  • 5 posts
THANK YOU SO MUCH!!

it works perfectly now with no errors :o)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users