Question

**how can I make my program break if 0 is entered by user as last number...

**how can I make my program break if 0 is entered by user as last number not always 10 numbers

output should be

enter 10 numbers:1 2 0
numbers entered by user are: 1 2
Largest number: 2

**arrays can ONLY be used in the main function, other than the main function pointers should be used

#include
#define N 10
void max(int a[], int n, int *max);
int main (void)
{
int a [N], i , big;
printf("enter %d numbers:",N);
for(i=0;i scanf("%d",&a[i]);
max(a,N,&big);
printf("numbers entered by user are:\n",&a[i]);
printf("Largest number:%d\n",big);
return 0;
}
void max(int a[], int n, int *max)
{
int i;
for(i=1;i if(a[i]>*max)
*max=a[i];
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#define N 10

void max(int a[], int n, int *max);

int main (void)
{
int a[N], i , big,count;
count = 0;
printf("enter %d numbers:",N);
for(i=0;i<N;i++)
{
scanf("%d",&a[i]); //enter numbers

if(a[i] != 0)   //if number is not equal to 0 increment count,the actual numbers entered
count++;
else            //otherwise break out of loop
break;
}
max(a,count,&big); //function call with actual count of numbers

printf("\nnumbers entered by user are:\n",a[i]);

for(i=0;i<count;i++)
printf("%d\t",a[i]);   //display numbers entered

printf("\nLargest number:%d\n",big); //diaplay largest number
return 0;
}

void max(int a[], int count, int *max)
{
int i;
*max = 0;
for(i=0;i<count;i++)
{
if(a[i]>*max)    //check if number at index i is greater than max
*max=a[i];         // if true assign the number to max
}
}


Output:

enter 10 numbers:1 2 0
numbers entered by user are:

1 2
Largest number: 2

Add a comment
Know the answer?
Add Answer to:
**how can I make my program break if 0 is entered by user as last number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My code is giving me errors output enter numbers ending with 0: 1 2 4 3...

    My code is giving me errors output enter numbers ending with 0: 1 2 4 3 0 numbers entered by user are: 1 2 4 3 the max(largest) number is: 4 Use pointers. Arrays cannot be used except on the main function. #include #define N 10 void main() { int max,a[N]={0}; int getseries(*&a[N]); findmax(&a,N); } void getseries(int *p,int P) { int *q; printf("enter numbers ending with 0: "); for(q=p;q<=p+P;q++) { scanf("%d",q); if (*q=0) break; } } printf("numbers entered by user...

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • The following program sorts the array of numbers entered by the user and prints out the...

    The following program sorts the array of numbers entered by the user and prints out the elements in descending order. It has many errors. Debug the code and show the output. #include <stdio.h> #define N 4 void desc(float *a, float *b) float tp; tp=a; a=b; b=tp; int main() int i, j, ii, mark[N] ; for (ii==0;ii<N;ii++) printf("Enter the %d th element of your array: \n",ii); scanf("%d", mark[ii]); printf("You have entered the following array: \n"); for (ii=0,ii<N,ii++) printf("%d ", mark[ii]); printf("\n...

  • Write a complete C program for an automatic teller machine that dispenses money. The user should...

    Write a complete C program for an automatic teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispenses are 50s, 20s, and 10s. Write a function that determines how many of each kind of bill to dispense. When writing your function begin to pass your variables using pointers (or rather "pointing" to your data". Use the TimeSpace program...

  • I'm having trouble understanding pointers in my c programming class. I posted the pointer assignment below....

    I'm having trouble understanding pointers in my c programming class. I posted the pointer assignment below. If you could leave comments pointing out where pointers are used it would be much appreciated! ----------------------------------------------------------------------------------------------------------------------------------- Write a complete C program for an automatic teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispenses are 50s, 20s, and 10s. Write a...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT