-3
#include<stdio.h>
int main(){
  int a[50],size,i,big,small;

  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);

  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i])
           big=a[i];
  }
  printf("Largest element: %d",big);

  small=a[0];
  for(i=1;i<size;i++){
      if(small>a[i])
           small=a[i];
  }
  printf("Smallest element: %d",small);

  return 0;
}

I wrote this code in C language which should find the average, longest character and shortest character from the keyboard...Also this program should terminate when the user input asterisk(*)... I cannot figure it out how should I do the average but I did do something to find longest and shortest character.. Please help???

Rex Ryan
  • 1
  • 6
  • Exactly what part of finding the average is giving you trouble? – bdonlan Oct 24 '11 at 04:02
  • If user input a characters and then the program should find its average??? – Rex Ryan Oct 24 '11 at 04:03
  • No I am trying to play with it, so I can increase my knowledge... – Rex Ryan Oct 24 '11 at 04:03
  • 1
    What I mean is, what part of finding an average is causing you trouble? This looks a lot like homework, so I don't want to just give you the code, so I'd like to narrow in on what's giving you trouble. For example, do you know how averages are calculated by hand? – bdonlan Oct 24 '11 at 04:05
  • Yes I do and I can input that code but how can I show to the user on the screen?? – Rex Ryan Oct 24 '11 at 04:08
  • Also does characters include like a normal words or jus digits?? – Rex Ryan Oct 24 '11 at 04:08
  • 2
    I don't think you actually wrote this code. The whitespace and brace stylings are consistent within this code sample, but totally different from the sample in [your other program](http://stackoverflow.com/questions/7871073/use-scanf-instead-of-fgets-and-fputs-in-my-program-in-c), which you claim you also wrote. There you use whitespace around everything, here you use almost as little as possible. Where are you getting this code, and what are you trying to do with it? – Chris Lutz Oct 24 '11 at 04:10
  • Where exactly are you trying to average? I can't see sign of it in your code. – Lionel Oct 24 '11 at 04:10
  • i want to add the code which can calculate the average?? – Rex Ryan Oct 24 '11 at 04:14
  • 2
    @RexRyan: Answer the questions posed. Average of _what_? – Lightness Races in Orbit Oct 24 '11 at 04:26
  • 1
    the OP's code is very similar to that posted here: http://codewordblog.blogspot.com/2011/07/find-largest-number-in-list.html - it also seems to be popular to copy: http://c.ittoolbox.com/groups/technical-functional/c-l/problem-in-class-array-4327073 – bdonlan Oct 24 '11 at 04:35
  • @ChrisLutz, note also how the OP immediately edited out the code from his post after you pointed that out. I guess he doesn't know about the edit history. – bdonlan Oct 24 '11 at 04:41
  • 1
    I found the longest character on my keyboard. It was the space bar! – makes Oct 24 '11 at 06:53

1 Answers1

2

This does look like homework, but to begin with, there's nothing stopping it from returning 0 after the function does the work, so it'll always terminate.

You'll have to put a check for input, to see if a user types in an asterisk, and then return 0, to stay similar to the way you have it.

Mike
  • 662
  • 3
  • 13
  • 27
  • can u explain more?? first of all this is not a HW – Rex Ryan Oct 24 '11 at 04:12
  • You want the program to end if the user inputs an asterisk, right? So check the input against an asterisk, and if the input is equal to an asterisk, terminate the program. – Mike Oct 24 '11 at 04:17
  • yes thats what i want but what code should i typed in?? – Rex Ryan Oct 24 '11 at 04:20
  • 2
    @RexRyan - Stack Overflow is not a "gimmeh teh codez!!!1" type of site. Please ask **specific questions** about **specific snippets of code** that you're having trouble with. –  Oct 24 '11 at 04:22
  • Do you know how to check for equality in C? That and maybe a loop will take care of it. – Mike Oct 24 '11 at 04:22
  • 2
    @RexRyan - Then please learn about how to compare strings and integers for equality in C, rephrase your question to make it more coherent, focus in on a particular snippet of code that's giving you trouble (if any of it still is at that point), and then ask **specific questions**. Oh, and when other users ask questions about your questions, **answer them**. –  Oct 24 '11 at 04:38