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]);
}
Related
#include <stdio.h>
typedef struct book
{
int bid;
char bname[50];
} bok;
typedef struct student
{
int roll;
char sec;
char name[50];
bok *issue;
} stud;
int main()
{
int n;
printf("enter no. of students\n");
scanf("%d", &n);
stud s[n];
printf("enter names,section and roll no. of %d students\n", n);
for (int i = 0; i < n; i++)
{
scanf("%s", &s[i].name);
scanf("%c", &s[i].sec);
scanf("%d", &s[i].roll);
}
int m, temp;
printf("enter no. of books\n");
scanf("%d", &m);
bok s1[m];
printf("enter book id and book names of %d books\n", m);
for (int i = 0; i < m; i++)
{
scanf("%d\n", &s1[i].bid);
scanf("%s\n", &s1[i].bname);
}
printf("assign book id to students.\n");
for (int i = 0; i < n; i++)
{
printf("assign id for roll %d\n", s[i].roll);
scanf("%d\n", s[i].issue);
}
int info;
printf("enter roll no. of student to get his/her information and book assigned to him\n ");
scanf("%d\n", &info);
for (int i = 0; i < n; i++)
{
if (s[i].sec == info)
printf("name:%s \nsection:%c \nbook assigned:%s\n", s[i].name, s[i].sec, *s[i].issue);
}
}
my code is not showing any compilation error but it does not give required output.my question is Create 2 two structure array Student and Book by the name S and B. And link the students with their respective books.
Print the books issued by a particular student.
Do not forget the brackets in order to assign user's input to the corresponding struct member:
for (int i = 0; i < n; i++)
{
scanf("%s", &(s[i].name));
scanf("%c", &(s[i].sec));
scanf("%d", &(s[i].roll));
}
and here you need to assign the input to bid, you cannot set an integer for book:
scanf("%d\n", &(s[i]->issue.bid));
or you wil get a segfault.
I want to make a simple variable for number of the round for a loop, so I tried my code
int size,counter,marks[size];
scanf("enter %d/n",&size);
for(counter=0;counter<size;counter++)
{
scanf("%d",&marks[counter]);
}
and compiled with no error but in run, it just shows "process returned -1073741571 <0*c00000FD>.
so I tried gets function and it shows "too many arguments to function 'gets' ".
int size;
int counter;
int marks[size];
scanf("enter %d/n",&size);
for(counter=0;counter<size;counter++)
{
gets("%d",&marks[counter]);
}
I'm using code::blocks 17.12 and the gnu compiler.
size can have any value when the array marks is allocated because it is not initialized. The array might be smaller than the entered size and so marks are stored in non-allocated memory, giving you the error.
This is a possible solution, but it doesn't compile with strict ISO C90. Presumably your CodeBlocks uses GCC that accepts variable length arrays and mixed declarations and code.
#include <stdio.h>
int main(void) {
int size;
printf("enter size: ");
scanf("%d",&size);
int marks[size];
int counter;
for (counter = 0; counter < size; counter++) {
scanf("%d", &marks[counter]);
}
for (counter = 0; counter < size; counter++) {
printf("%d: %d\n", counter, marks[counter]);
}
return 0;
}
BTW, please don't say "build error" if you have a runtime error. ;-)
Please don't use gets. It's dangerous.
As for your error in the scanf example, the first problem is the line
int size,counter,marks[size];
which declares marks with the uninitialized size value. Try initializing size first, then declaring the marks array.
Your second problem is scanf formatting string. Use scanf to read formatted input, not output a prompt. Use puts or printf for that.
Here's a full example:
#include <stdio.h>
int main(void) {
int size;
printf("Enter a size value: ");
scanf("%d", &size);
int marks[size];
for (int i = 0; i < size; i++) {
printf("Enter element %d: ", i);
scanf("%d", &marks[i]);
}
printf("You entered: ");
for (int i = 0; i < size; i++) {
printf("%d ", marks[i]);
}
puts("");
return 0;
}
Here's a sample run:
Enter a size value: 4
Enter element 0: 88
Enter element 1: 77
Enter element 2: 66
Enter element 3: 55
You entered: 88 77 66 55
If you're writing ANSI C-compatible code you can use dynamic memory with malloc:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i, size, *marks;
printf("Enter a size value: ");
scanf("%d", &size);
if (size < 1) {
fprintf(stderr, "Invalid size specified\n");
exit(1);
}
marks = malloc(size * sizeof(int));
if (!marks) {
fprintf(stderr, "malloc failed\n");
exit(1);
}
for (i = 0; i < size; i++) {
printf("Enter element %d: ", i);
scanf("%d", &marks[i]);
}
printf("You entered: ");
for (i = 0; i < size; i++) {
printf("%d ", marks[i]);
}
free(marks);
puts("");
return 0;
}
size must have a defined value, for example:
#include <stdio.h>
int main()
{
int size;
size = 5; // size must be constant
int counter, marks[size];
for (counter = 0; counter < size; counter++)
{
scanf("%d", &marks[counter]);
}
//Printing it returns correct values:
for (counter = 0; counter < size; counter++)
{
printf("%d\n", marks[counter]);
}
}
You can instead input it's value from the user if you want.
However, if for some reason, size is to be defined after the array is declared, use pointers:
#include <stdio.h>
#include "stdlib.h"
int main()
{
int size;
int counter, *marks;
size = 5; //declared after the array
marks = (int *)malloc(size * sizeof(int));
for (counter = 0; counter < size; counter++)
{
scanf("%d", &marks[counter]);
}
//Printing it returns correct values:
for (counter = 0; counter < size; counter++)
{
printf("%d\n", marks[counter]);
}
//Don't forget to free the array in the end
free(marks);
}
So I want to sort the list so that i can give them their position according to their grades but it's not working.
It is Showing error like : incompatible types when assigning to type char from type struct info.
p.s : I am trying to do this sorting using Structures.
#include <stdio.h>
#define SIZE 3
struct info {
char name[20];
int number;
double grade;
};
int main(void) {
struct info list[SIZE];
int i, j;
char temp;
int avg;
for (i = 0; i < SIZE; i++) {
printf("Enter your name : ");
scanf("%s", list[i].name);
printf("Enter your number : ");
scanf("%d", & list[i].number);
printf("Enter your grade : ");
scanf("%lf", & list[i].grade);
}
for (i = 0; i < SIZE; i++) {
printf("Name : %s Student Number : %d Grade : %f", list[i].name,
list[i].number, list[i].grade);
printf("\n");
}
for (i = 0; i < SIZE; i++) {
for (j = i + 1; j < SIZE; j++) {
if (list[j].grade > list[i].grade) {
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
}
for (i = 0; i < SIZE; i++) {
printf("%d st Position : Name : %s Student Number : %d Grade : %f ",i,
list[i].name, list[i].number, list[i].grade);
printf("\n");
}
avg = 0;
for (i = 0; i < SIZE; i++)
avg += list[i].grade;
avg = avg / 3;
printf("Average is %d", avg);
getch();
return 0;
}
errors are in the following code:
for(i=0;i<SIZE;i++) {
for(j=i+1;j<SIZE;j++) {
if(list[j].grade>list[i].grade) {
temp=list[j];
list[j]=list[i];
list[i]=temp;
}
}
}
You assign a type char with name temp and try to assign a struct to it. Instead create a struct temp and then sort your array of structs.
#include <stdio.h>
#define SIZE 3
struct info { // use typedef struct info instead
char name[20];
int number;
double grade;
};
int main(void) {
struct info list[SIZE]; // allows you to write info list[size]
int i, j; // do not initialize loop variables outside of their loops unless you need to(makes it easier to read the loop statements/spot mistakes)
char temp; // you need to use a type info, not char e.g. info temp
I can not compile it myself at the moment, so please let us know in the comments if you have any further questions.
This code is supposed to recieve to arrays and then call function to return them in 1 array but I don't know how to print the last array returned from the function thanks in advance ???
and now I write anything because it says that the post is mostly code :D :D
#include <stdio.h>
#include <stdlib.h>
int join_arrays(int *array1, int *array2, int arr1_size, int arr2_size);
int main() {
int size_arr1, size_arr2, i, num1 = 1, s;
printf("Please enter the size of the first array: ");
scanf("%d", &size_arr1);
int arr1[size_arr1];
printf("start fill your first array: \n");
for (i = 0; i < size_arr1; i++) {
printf("enter element number %d: ",num1);
scanf("%d", &arr1[i]);
num1++;
}
num1 = 1;
printf("Please enter the size of the second array: ");
scanf("%d", &size_arr2);
int arr2[size_arr2];
int *ptr1_last;
printf("start fill your second array: \n");
for (i = 0; i < size_arr2; i++) {
printf("enter element number %d: ", num1);
scanf("%d", &arr2[i]);
num1++;
}
ptr1_last = join_arrays(arr1, arr2, size_arr1, size_arr2);
printf("sorted array= \n");
for (s = 0; s < (size_arr1 + size_arr2); s++) {
printf("%d\n", ptr1_last);
}
return 0;
}
int join_arrays(int *array1, int *array2, int arr1_size, int arr2_size) {
int counter_arr1, counter_arr2, m = 0;
int last_arr[arr1_size + arr2_size];
for (counter_arr1 = 0; counter_arr1 < arr1_size; counter_arr1++) {
last_arr[counter_arr1]=array1[counter_arr1];
}
for (counter_arr2 = counter_arr1; counter_arr2 < (arr1_size + arr2_size); counter_arr2++) {
last_arr[counter_arr2] = array2[m];
m++;
}
return last_arr[0];
}
Modified the code to create the receiving array in main and pass a pointer to it to the merge function because the local array last_arr would no longer exist when the function returned in your code.
#include <stdio.h>
#include <stdlib.h>
//Prototype changed to include a pointer to the receiving array, also no longer returns a value.
void join_arrays(int *last_arr, int *array1,int *array2,int arr1_size,int arr2_size);
int main()
{
int size_arr1,size_arr2,i,num1=1,s;
printf("Please enter the size of the first array: ");
scanf("%d",&size_arr1);
int arr1[size_arr1];
printf("start fill your first array: \n");
for(i=0; i<size_arr1; i++)
{
printf("enter element number %d: ",num1);
scanf("%d",&arr1[i]);
num1++;
}
num1=1;
printf("Please enter the size of the second array: ");
scanf("%d",&size_arr2);
int arr2[size_arr2];
int *ptr1_last;
printf("start fill your second array: \n");
for(i=0; i<size_arr2; i++)
{
printf("enter element number %d: ",num1);
scanf("%d",&arr2[i]);
num1++;
}
int last_arr[size_arr1 + size_arr2]; //Create receiving array here
join_arrays(last_arr, arr1,arr2,size_arr1,size_arr2); //And pass it to the function.
printf("merged array= \n");
for(s=0;s<(size_arr1+size_arr2);s++)
{
printf("%d\n", last_arr[s]);
}
return 0;
}
void join_arrays(int *last_arr, int *array1,int *array2,int arr1_size,int arr2_size)
{
int counter_arr1, m=0;
for(counter_arr1=0; counter_arr1<arr1_size; counter_arr1++)
{
last_arr[counter_arr1]=array1[counter_arr1];
}
for(; counter_arr1<(arr1_size+arr2_size); counter_arr1++)
{
last_arr[counter_arr1]=array2[m];
m++;
}
}
With that function you return only the first element of the last_array, you should create a global array so it's visible in all functions, or return a pointer of the last_array[0] position in memory
#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);
}