How to find the Highest mark for giving three marks? - c

#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);
}

Related

How to have variables of scores

I have a question, how can I get a variable of scores from user and take that variable to divide to get the average number, I saw I could change line 19, the scores[] to something and the number to divide, can anyone show me how to do it ?
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n = get_int("How many scores you have? ");
int score[n];
for( int i = 0; i < n; i++)
{
score[i] = get_int("Score: ");
}
printf("Average: %.2f\n", (score[n] + score[n]+ score[n]) / 3.0);
}
declare int i before the first loop make another loop to add up all the scores and divide at the end by i (the number of scores) to get the average.
like this:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n = get_int("How many scores you have? ");
int score[n];
double average = 0;
for (int i = 0; i < n; i++)
{
score[i] = get_int("Score: ");
average+=score[i] // if you just need the average you can delete the line int score[n] and the line above and change score[i] to get_int("Score: ");
}
average = average / n;
printf("Average: %.2f\n", average);
}

How to put commas on a series of numbers in a proper way

Yes I have searched through the net and also here but failed to find similar cases. I have here a program which prints prime numbers between 2 given integers. But I have to print them with commas in a proper way. like 1, 2, 3, 4 without having a comma on the last integer. Here is my code.
#include<stdio.h>
void main()
{
int x, y, i, j;
puts("Enter first number: ");
scanf("%d", &x);
puts("Enter second number: ");
scanf("%d", &y);
for(i=x; i<=y; i++)
{
for(j=2; j<=i; j++)
{if(i%j==0)
{
break;
}
}
if(i==j)
{
printf("%d, ", i);
}
}
}
I wanted to identify the total number of prime numbers printed in order to set a condition that if it is the last one, a comma will not print but I don't know if it will work. That is the only thing I can think of for now and your help guys will be really appreciated. Thank you!
As #kaylum pointed out, use a flag to "skip" printing the comma when printing the first number. Set that flag to false after the very first number you print.
#include <stdio.h>
void main() {
int x, y, i, j;
puts("Enter first number: ");
scanf("%d", &x);
puts("Enter second number: ");
scanf("%d", &y);
int is_first_time = 1;
for (i = x; i <= y; i++) {
for (j = 2; j <= i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
if (is_first_time) {
printf("%d", i);
is_first_time = 0;
}
else {
printf(", %d", i);
}
}
}
}
You can use a flag, as others have suggested, but you can also use a string as a separator, setting it to empty for the first element and any desired separator after that.
#include <stdio.h>
void print_join(const char *sep, int n, int a[n])
{
const char *s = "";
for (int i = 0; i < n; i++)
{
printf("%s%d", s, i);
s = sep;
}
printf("\n");
}
int main(void)
{
int a[] = {1, 2, 3, 4, 5};
int n = sizeof a / sizeof *a;
print_join(", ", n, a);
print_join(";", n, a);
print_join("", n, a);
print_join("--", n, a);
return 0;
}

Can anyone notice where the mistakes is in my code?

Can anyone tell me why is my output wrong "1* 3 = 1" whereas other multiplications is correct? Thanks!
#include <stdio.h>
int main()
{
int n,i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n",n);
while(i<= 12)
{
printf("\n%d*%d=%d", i, n, total);
i++;
total = i*n;
}
}
You are printing a value, total, before you assign any value to it. On the first iteration of the while loop, total doesn't contain any defined value.
Here's a way to fix it:
#include <stdio.h>
int main(void)
{
int n, i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n", n);
while (i <= 12)
{
total= i * n;
printf("\n%d*%d=%d", i, n, total);
i++;
}
}
Simply calculate total before you print it, not after.

how can i solve segmentation error in terminal message?

I wrote a simple program to take names and numbers from the user and store it in an array, then compare between each cell until it reach the maximum grade then it displays it. The problem is that when it run it shows a message (segmentation fault (Core dump)). I really don't know what my mistake is.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", & n);
char name[n];
float score[n];
for (int i = 0; i < n; i++) {
printf("\nEnter the name of Student: ");
scanf("%s", & name[i]);
printf("\nEnter the score: ");
scanf("%f", & score[i]);
}
float max;
int index;
max = score[0];
index = 0;
for (int i = 1; i < n; i++) {
if (max < score[i]) {
max = score[i];
index = i;
}
}
printf("\nHighest mark scored is %f by student %s", name[index], score[index]);
}
1- you use user input to define the size of an array (wrong)
-- array in c has static size so you must define it's size before the code reach the compiling stage(use dynamic memory allocation instead)
2- scanf("%s", & name[I]); you want to save array of char and save the address at the name variable but name it self not a pointer to save address it's just of char type (wrong)
-- you need pointer to save the address of every name so it's array of pointer and a pointer to allocate the address of the array to it so it's a pointer to poiner and define max size of word if you define size the user input exceed it will produce an error
3- finally you exchanged the %f,%s in printf
#include <stdlib.h>
#include <stdio.h>
#define SIZE_OF_WORD 10
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", & n);
char **name=(char**)malloc((sizeof(char*)*n));
for (int i = 0; i < n; i++) {
name[i]=(char*)malloc((sizeof(char)*SIZE_OF_WORD));
}
float *score=(float*)malloc((sizeof(float)*n));
for (int i = 0; i < n; i++) {
printf("\nEnter the name of Student: ");
scanf("%s", name[i]);
printf("\nEnter the score: ");
scanf("%f", & score[i]);
}
float max;
int index;
max = score[0];
index = 0;
for (int i = 1; i < n; i++) {
if (max < score[i]) {
max = score[i];
index = i;
}
}
printf("\nHighest mark scored is %s by student %.0f\n", name[index],score[index]);
}

Printing positions in field for same numbers, c language

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;
}
}

Resources