we got a homework in school to make this type of programme I share here. I have it done its working but I need this: If I put 2 or more same lowest or highest numbers I need to print position of all same lowest and highest numbers. And I am stucked here. Can you help me, Thanks
code:
#include <stdio.h>
int main(void)
{
int pole[100];
int i, max_cislo, min_cislo, x, max, min;
printf("Napis, kolko cisel chces ulozit do pola :");
scanf("%d",&x);
printf("Vloz %d cisla do pola :\n",x);
for(i=0;i<x;i++){
printf("cislo %d -: ",i);
scanf("%d",&pole[i]);
}
max_cislo = pole[0];
min_cislo = pole[0];
for(i=1; i<x; i++){
if(pole[i]>max_cislo){
max_cislo = pole[i];
max = i;
}
if(pole[i]<min_cislo){
min_cislo = pole[i];
min = i;
}
}
printf("Maximalny prvok je : %d a jeho pozicia v poli je: %d\n", max_cislo, max);
printf("Minimalny prvok je : %d a jeho pozicia v poli je: %d\n", min_cislo, min);
return 0;
}
You should:
Allocate an array to store all min/max positions.
Remove all elements and store new position if the record is breaked.
Add new position to the list if the record is tie.
It will be like this (only max is shown, min can also be done like this):
int pole[100], x;
int i, max_poses[100], max_pos_count = 0, max = 0;
/* read things to pole and x */
max = pole[0];
max_poses[0] = 0;
max_pos_count = 1;
for (i = 1; i < x; i++) {
if (pole[i] > max) {
/* the record is broken */
max = pole[i];
max_poses[0] = i;
max_pos_count = 1;
} else if (pole[i] == max) {
/* tie */
max_poses[max_pos_count++] = i;
}
}
Related
I have to calculate the arithmetic and geometrical mean of numbers entered by the user in C language. The algorithm works fine, but I don't know how to do the enter numbers until 0 is pressed part. I have tried many things but nothing works. Here is what I have tried to do until now. Thanks for the help.
int main() {
int n, i, m, j, arr[50], sum = 0, prod = 1;
printf("Enter numbers until you press number 0:");
scanf("%d",&n);
while (n != 0) {
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum = sum + arr[i];
prod = prod * arr[i];
}
}
int armean = sum / n;
float geomean = pow(prod, (float)1 / n);
printf("Arithmetic Mean = %d\n", armean);
printf("Geometric Mean = %f\n", geomean);
getch();
}
Your code is asking for the number of values in advance and subsequently reading that many values. That's not what you were asked to do.
You need to ask for numbers in a loop and exit the loop when the number that you read is 0. You don't even need an array:
int n = 0, i, m, j, sum=0, prod=1;
while (1) {
int value;
scanf("%d",&value);
if (value == 0) {
break;
}
sum=sum+value;
prod=prod*value;
n++;
}
int armean=sum/n;
float geomean=pow(prod,(float) 1/n);
You have to break the for loop when value 0 entered; so you should check for arr[i].
While loop is not required.
Please go through below code; this could be help full:
#include <stdio.h>
int main()
{
int n, i, m, j, arr[50], sum=0, prod=1;
printf("Enter numbers until you press number 0:");
for(i=0; i<50; i++)
{
scanf("%d",&arr[i]);
if (arr[i] == 0)
{
break;
}
sum=sum+arr[i];
prod=prod*arr[i];
}
printf ("%d %d\n",sum, prod);
n = i+1;
int armean=sum/n;
float geomean=pow(prod,(float) 1/n);
printf("Arithmetic Mean = %d\n",armean);
printf("Geometric Mean = %f\n",geomean);
getch();
return 0;
}
what dbush said is right, you don't need array and are not asking the number in advance but what he did not tell is how can you find the number of values
int main()
{
int n, sum=0, prod=1, num;
printf("Enter numbers until you press number 0:\n");
for(n=0; ; n++)
{
scanf("%d",&num);
if(num==0)
break;
sum=sum+num;
prod=prod*num;
}
printf("sum is %d \n",sum);
printf("prod is %d \n",prod);
printf("n is %d \n",n);
float armean=sum/n; //why int?
float geomean=pow(prod,(float) 1/n);
printf("Arithmetic Mean = %d\n",armean);
printf("Geometric Mean = %f\n",geomean);
//getch(); why getch(), you are not using turboc are you?
}
There is no need for an array, but you should test if the number entered in 0 after reading it from the user. It would be better also to use floating point arithmetic to avoid arithmetic overflow, which would occur quickly on the product of values.
In any case, you must include <math.h> for pow to be correctly defined, you should test the return value of scanf() and avoid dividing by 0 if no numbers were entered before 0.
#include <stdio.h>
#include <math.h>
int main() {
int n = 0;
double value, sum = 0, product = 1;
printf("Enter numbers, end with 0: ");
while (scanf("%lf", &value) == 1 && value != 0) {
sum += value;
product *= value;
n++;
}
if (n > 0) {
printf("Arithmetic mean = %g\n", sum / n);
printf("Geometric mean = %g\n", pow(product, 1.0 / n));
getch();
}
return 0;
}
I can't find the mistake here, I try to insert tha tMax array to the avgMax function.
Error is: Called object 'tMax' it's not a function or function pointer...
My code is :
#include <stdio.h>
int avgMax(int tMax[6])
{
int i,avgH=0;
for(i=0;i<6;i++)
{
avgH +=tMax[i];
}
return avgH;
}
int main()
{
int tMax[6],tMin[6],i,j,avgH;
avgH=0;
for(i=0;i<6;i++)
{
printf("Temperatura maxima din %d zi : \n", i+1);
scanf(" %d",&tMax[i]);
}
for(j=0;j<6;j++)
{
printf("Temperatura minima din %d zi : \n", j+1);
scanf(" %d",&tMin[j]);
}
for(i=0;i<6;i++)
{
avgH +=tMax[i];
}
printf("Average temperature max = %d",avgMax(tMax(6)));
return 0;
}
In C, when you pass an array to a function it is converted to a pointer, so the length that you specify in the formal parameter tMax is ignored by the compiler. The recommended approach is to pass the length of the array as a separate parameter. In the modified version below I also use a most convenient LEN macro:
#include <stdio.h>
#define LEN(arr) (sizeof (arr) / sizeof (arr)[0])
int avgMax(const int tMax[], int tMaxLen)
{
int i, avgH = 0;
for (i = 0; i < tMaxLen; i++) {
avgH += tMax[i];
}
return avgH;
}
int main()
{
int tMax[6], tMin[6], i, j, avgH;
avgH = 0;
for (i = 0; i < LEN(tMax); i++) {
printf("Temperatura maxima din %d zi : \n", i + 1);
scanf(" %d", &tMax[i]);
}
for (j = 0; j < LEN(tMin); j++) {
printf("Temperatura minima din %d zi : \n", j + 1);
scanf(" %d", &tMin[j]);
}
for (i = 0; i < LEN(tMax); i++) {
avgH += tMax[i];
}
printf("Average temperature max = %d\n", avgMax(tMax, LEN(tMax)));
return 0;
}
The last but one line:
printf("Average temperature max = %d",avgMax(tMax(6)));
Should be :
printf("Average temperature max = %d",avgMax(tMax));
The problem comes from here :
printf("Average temperature max = %d",avgMax(tMax(6)));
with this syntax tMax(6) you are trying to call a function named tMax.
To pass your array as an argument change the line by :
printf("Average temperature max = %d",avgMax(tMax));
#include <stdio.h>
#include<conio.h>
int main ()
{
printf("Enter the Physics ,Chemistry and Maths Marks");
int mark[3]= {40,50,10};
int s[3];
int i;
int sum = 0, highest = 0;
clrscr();
for (i = 0; i < ; i++)
{
sum += mark[i];
if (mark[i] > highest)
highest = mark[i];
}
printf("The Highest Mark is %d: \n", highest);
getch();
return 0;
}
its working fine, I need to give a input dynamically and get the output
How to do that?
Enter the Marks : 30 20 10
output: 30
This:
int mark[3];
mark[3] = scanf("%d",mark[3]);
is wrong because of several reasons. It seems that you don't know how to use scanf properly. It should be
int mark[3];
scanf("%d", &mark[0]);
scanf("%d", &mark[1]);
scanf("%d", &mark[2]); /* Get each number from stdin and store it in the address of the variable given */
or better
int mark[3];
scanf("%d %d %d", &mark[0], &mark[1], &mark[2]);
or even better
int mark[3], i;
for(i = 0; i < 3; i++) /* Loop 3 times */
{
scanf("%d", &mark[i]);
}
You can get the numbers from the input using scanf() and then find the highest number using fmax():
#include <stdio.h>
#include <math.h>
int main()
{
int mark[3] = {0, 0, 0};
int highest = 0;
printf("Enter the Marks\n");
for (int i = 0; i < 3; i++) {
printf("%d: ", i + 1);
scanf("%d", &mark[i]);
highest = fmax(highest, mark[i]);
}
printf("The Highest Mark is %d: \n", highest);
}
*EDITED*
I fixed some issues but i'm still calling it wrong. Somehow when i don't declare with int the GetRand function more than once i get more error messages.
What i want as a final result is to print the array i created and also print the maximum and average of the values of it (only counting every number > -1).
I'm calling the maxavg() function wrong and i'm getting an error message "Error] expected identifier or '(' before '{' token" at the beginning of maxavg which i haven't been able to fix.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int GetRand(int min, int max);
int maxavg();
int main ()
{
int a[21][21], i , j, average, maximum;
for (i = 0; i < 21; i++)
{
for ( j = 0; j < 21; j++)
{
a[i][j] = GetRand(0, 100);
printf("%3d" , a[i][j]);
}
a[2][15] = -1;
a[10][6] = -1;
a[13][5] = -1;
a[15][17] = -1;
a[17][17] = -1;
a[19][6] = -1;
printf("\n");
}
average = maxavg();
maximum = maxavg();
printf("average = %d \n maximum = %d", average, maximum);
return 0;
}
// random seed
int GetRand(int min, int max);
int get ()
{
int i, r;
for (i = 0; i < 21; i++)
{
r = GetRand(0, 100);
printf("Your number is %d \n", r);
}
return(0);
}
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
srand(time(NULL));
Init = 1;
}
rc = (rand() % (max - min +1) +min);
return (rc);
}
// max and average
int maxavg();
{
int max=INT_MIN, sum=0, count=0, avg, n, m, current;
current = a[i][j];
avg = sum/count;
for(n = 0; n < 21; n++){
for(m =0; m < 21; m++){
if(current > -1){
sum = sum + current;
count = count + 1;
if(current > max){
max = current;
}
}
}
}
return(0);
}
This program will only print the elements of the array a in the for-loop in main. Apart from GetRand, no other function is called, so get and maxavg are never executed, despite being defined. So yes, you should first of all call it from main if you want to see what it does.
There is also a big problem with the logic of your maxavg function though.
Where is the array you're presuming to iterate over? You haven't passed any parameter to maxavg (nor declared and set a local variable). It looks like you're expecting current to contain these array element values, but the reality is you've never set the value of this variable to anything. You should be using the i and j variables as indexes into the array you should add, as in arr[i][j].
A few other notes:
You really should be setting a[2][15], a[10][6] and so on after the loop has finished, not in every single iteration.
You've declared GetRand twice.
maxavg returns an int, yet there is no return statement in your function ("control reaches end of non-void function").
I am trying to implement a simple tournament in C.
#include <stdio.h>
int main(void) {
int tourn[100], n, i;
printf("Give n:");
scanf("%d", &n);
printf("\n n = %d \n", n);
for(i = n; i <= (2*n)-1; i++)
scanf("%d", &tourn[i]);
build(tourn, n);
printf("\n Max = %d \n",tourn[1]);
printf("\n Next Max = %d \n",nextmax(tourn, n));
}
void build(int tourn[], int n) {
int i;
for(i = 2*n-2; i > 1; i = i-2)
tourn[i/2] = max(tourn[i], tourn[i+1]);
}
int nextmax(int tourn[],int n) {
int i = 2;
int next;
next = min(tourn[2], tourn[3]);
while(i <= 2*n-1) {
if(tourn[i] > tourn[i+1]) {
next = max(tourn[i+1], next);
i = 2*i;
}
else {
next = max(tourn[i], next);
i = 2*(i+1);
}
}
return(next);
}
int max(int i,int j) {
if(i > j)
return i;
else
return j;
}
int min(int i,int j) {
if(i < j)
return i;
else
return j;
}
The output for n = 5 and
1 2 3 4 5
is
Max = 4195048
Next Max = 32588
and this output varies each time by a small amount!
if I place a test printf command before the build function, it doesn't execute.
Can someone find the error/explain the output?
Thanks :)
Your code seems pretty broken to me. you don't mind to address beyond your array boundaries, which is a good way of producing random results:
while(i <= 2*n-1){
if(tourn[i]>tourn[i+1]){
next = max(tourn[i+1],next);
i=2*i;
} else {
next = max(tourn[i],next);
i=2*(i+1);
}
}
Your (logical) array is of size 2n. if i reaches the "highest" value, you test tourn[i + 1], which is tourn[2n].