I have been searching for the problem with this code but I'm just not sure what to do now. I was doing some practice with structures in C.
It's intended to be a program that gets the values of 2 integers and adds them and stores the value in sum and then print sum.
#include <stdio.h>
#include <string.h>
int main()
{
typedef struct {
int num;
int num2;
int sum;
} calc;
struct calc a;
printf("Enter number: ");
scanf("%d", &a.num);
printf("\nEnter a number: ");
scanf("%d", &a.num2);
a.sum=a.num+a.num2;
printf("Output: %d", a.sum);
return 0;
}
Error: Storage size of a isn't known.
typedef struct {
int num;
int num2;
int sum;
} calc;
Here, you are giving an anomymous structure an alias of calc, so there's no such structure called struct calc. You should define a as:
calc a;
Or, you can give your structure a tag:
typedef struct calc {
int num;
int num2;
int sum;
} calc;
Now you can use either calc or struct calc.
You have typedef'ed your struct, so when you declare it, just use calc instead of struct calc.
Change struct calc a; to calc a;
Related
I had came across a problem that I have tried so many times with different ways but still not able to obtain the required solution please do help me.
PROBLEM: Define a structure to store students’ details. Pass them to a function where the student with HIGHEST CGPA is calculated from a set of 5 students. And display the result.(Here we have to store name, age and CGPA obtained of each student)
Here is my try at the problem:
#include <stdio.h>
void highCGPA(float b,struct detst D[4]);
struct detst
{
int age;
float CGPA;
char name[30];
};
int main()
{
struct detst D[4];
int i;
float h;
for(i=0;i<=4;i++)
{
printf("Enter the name of student %d:\n",i+1);
scanf("%s",&D[i].name);
printf("Enter the age of student %d:\n",i+1);
scanf("%d",&D[i].age);
printf("Enter the CGPA obtined by student %d:\n",i+1);
scanf("%f",&D[i].CGPA);
}
highCGPA(h,D);
}
void highCGPA(float b,struct detst D[4])
{
int i,max;
max = D[0].CGPA;
for(i=0;i<=4;i++)
{
if(D[i].CGPA > max)
{
max = D[i].CGPA;
}
}
printf("Highest CGPA obtained is:\n%f",max);
}
There is more than one problem. You're trying to store the data of 5 students in an array of struct with size 4. So:
struct detst D[5]; // fixed size
int i;
float h;
for(i=0;i<5;i++) { // EDIT (comments suggestion): for loop fix following standard convetions
/* take input */
}
Lastly, if you declare max as an int, you can't try to print it with a format specifier for float. So:
printf("Highest CGPA obtained is: %d\n",max); // format specifier for int is %d (not %f)
There are couple of other problems.
your declaration of funtion highCGPA is above your struct detst definition
you should have got error
error: array type has incomplete element type ‘struct detst’
declare your function after the structure definition.
your max variable is int in the function highCGPA , but you are trying to store a float value in it.
With a sligth change to program functionallity can be improved:
#include <float.h> // FLT_MAX
#include <stdio.h>
struct detst
{
int age;
float CGPA;
char name[30];
};
// Return offset of student with highest CGPA
int highCGPA(struct detst const (*D)[5]); // Using pointer to array
// to avoid degeneration
int main()
{
struct detst D[5];
int i;
for(i = 0; i < 5; i++)
{
printf("Enter the name of student %d:\n", i + 1);
scanf("%29s", D[i].name); // Make sure we don't overwrite the buffer
printf("Enter the age of student %d:\n", i + 1);
scanf("%d", &D[i].age);
printf("Enter the CGPA obtained by student %d:\n", i + 1);
scanf("%f", &D[i].CGPA);
}
int bestStudentOffset = highCGPA(&D); // By asking for best student
// we can obtain a lot more
printf(
"Best student is %d year old %s with\n",
D[bestStudentOffset].age,
D[bestStudentOffset].name);
printf("Highest CGPA obtained is:\n%f\n", D[bestStudentOffset].CGPA);
}
// Return offset of student with highest CGPA
int highCGPA(struct detst const (*D)[5])
{
int i, maxOffset = -1;
float max = -FLT_MAX;
for(i = 0; i < 5; i++)
{
if((*D)[i].CGPA > max)
{
max = (*D)[i].CGPA;
maxOffset = i;
}
}
return maxOffset;
}
I can't figure what is wrong here. This is supposed to be a function to read from the user the size of array and then pass it to function to start fill the array
#include <stdio.h>
#include <stdlib.h>
int input_array(int *start, int s_size);
int main()
{
int arr_size;
printf("Please enter the Size of your array: ");
scanf("%d",&arr_size);
int arr[arr_size];
input_array(arr,arr_size);
return 0;
}
int input_array(int *start, int s_size)
{
static int counter=0;
printf("Start fill your array with %d elements: \n\n",s_size);
for(counter=0; counter<s_size; counter++)
{
printf("Input Element : ");
scanf("%d",start[counter]);
printf("\n");
}
return start[0];
}
In you function int input_array(int *start, int s_size) this statement
As start is int * -
scanf("%d",start[counter]); // pass address of variable
you need to pass address of start[counter] because it is of type int .
I found the answer that this line
scanf("%d",start[counter]);
should be
scanf("%d",&start[counter]);
the and '&' operator was missig
This is a simple enough problem I'm trying to figure out how to pass the int variables back to the other functions from the input function so it can be used in the stuff function to do the math then it returns the added variable and then passes all three to the output function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int input(int first, int second, int third);
int stuff(int first, int second, int third, int added);
int output(int first, int second, int third, int added);
int main(){
int first,second,third;
int added;
//sub functions
input(first,second,third);
stuff(first, second, third, added);
output(first, second, third, added);
return(0);
}
int input(int first, int second, int third){
printf("Enter an interger for #1:");
scanf("%d",&first);
printf("Enter an interger for #2:");
scanf("%d",&second);
printf("Enter and interger for #3:");
scanf("%d",&third);
return first,second,third;
}
int stuff(int first, int second, int third, int added){
added = first + second + third;
return added;
}
int output(int first, int second, int third, int added){
printf("Integer 1 = %d\n",first);
printf("Integer 2 = %d\n",second);
printf("Integer 3 = %d\n",third);
printf("Integer 1,2,3 added together = %d\n",added);
}
Either use a struct
struct Foo {
int first, second, third;
}
struct Foo input() {
struct Foo foo;
printf("Enter an interger for #1:");
scanf("%d",&foo.first);
printf("Enter an interger for #2:");
scanf("%d",&foo.second);
printf("Enter and interger for #3:");
scanf("%d",&foo.third);
return foo;
}
Or pass pointers:
void input(int* first, int* second, int* third){
printf("Enter an interger for #1:");
scanf("%d",first);
printf("Enter an interger for #2:");
scanf("%d",second);
printf("Enter and interger for #3:");
scanf("%d",third);
}
int main(){
int first,second,third;
int added;
//sub functions
input(&first,&second,&third);
stuff(first, second, third, added);
output(first, second, third, added);
return(0);
}
You can use pointers and pass the address of those variables into each function. Every time you use it, you can dereference each variable in the functions.
You could just use pointers. I.e., change the function declaration to:
int input(int *first, int *second, int *third);
In the function itself, you use:
int input(int *first, int *second, int *third){
printf("Enter an interger for #1:");
scanf("%d",first);
printf("Enter an interger for #2:");
scanf("%d",second);
printf("Enter and interger for #3:");
scanf("%d",third);
}
And when you call it, use:
input(&first,&second,&third);
You could do the following:
void input(int *a, int *b, int *c);
int main()
{
int first, second, third;
/* ... */
input(&first, &second, &third);
/* ... */
}
void input(int *a, int *b, int *c)
{
printf("Enter an interger for #1:");
scanf("%d", a);
printf("Enter an interger for #2:");
scanf("%d", b);
printf("Enter and interger for #3:");
scanf("%d", c);
}
How to get the max value in a structure? I have tried to create a simple program but I'm having issues with the if statement or variable, since I cannot determine the winner or candidate number which has the highest score.
#include<stdio.h>
#include<conio.h>
struct Candidate{
float Score;
short Number;
}candidate1[5];
main(){
int i,n,highest;
for(i=0;i<5;i++){
printf("Candidate Number: ");
scanf("%i",&candidate1[i].Number);
printf("Score: ");
scanf("%i",&candidate1[i].Score);
highest=candidate1[i].Score;
for (n=0;n<5;n++)
if (candidate1[i].Score>highest);
}
printf("Highest Number: %i\n",highest);
system("pause");
}
fix like this
#include <stdio.h>
#include <stdlib.h>
struct Candidate {
float Score;
short Number;
}candidate1[5];
int main(void){
int i, n, highest = 0;
n = sizeof(candidate1)/sizeof(*candidate1);
for(i=0;i<n;++i){
printf("Candidate Number: ");
scanf("%hi", &candidate1[i].Number);
printf("Score: ");
scanf("%f",&candidate1[i].Score);
if(candidate1[highest].Score < candidate1[i].Score)
highest = i;
}
printf("Highest Number: %f\n", candidate1[highest].Score);
system("pause");
return 0;
}
,
The most common idiom for finding the max of type <T> (double, int, whatever) from an array of structs <S> goes as follows
<S> array[numElements]; //defined and allocated earlier. Has a score `<T> score`
<T> bestScore = array[0].score;
int bestIndex = 0;
int i;
for(i=1;i<numElements;i++) {
if(array[i].score>bestScore) {
bestIndex = i;
bestScore = array[i].score;
}
}
//do things with bestScore and array[bestIndex];
If you have a 2D array you can replace the single for-loop with a double for loop, and use an additional bestIndex variable for the second dimension.
If the array may contain 0 elements it is sometimes common to set bestScore to a minimum possible value and loop from 0 to numElements.
i have the following struct:
typedef struct number
{
int x;
int y;
int z;
unsigned long int final;
}number;
my code is the following:
number* numbers;
numbers= (number*)malloc(sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, &numbers->y, &numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d",numbers->final);
but the output is wrong. for example here is a run:
12 12 12
input: 12,12,12
final: -28640
i cant figure out the problem. the highest number that number->final can get is 90,000 (i make sure of it as i gives the input)... i seems like there is overlap? please help.
Your problem is the pointer. I am assuming you initialised the struct as follows.
numbers *numbers;
However if you use it in the main where you declare it don't use a pointer. There are also a few errors in your printf call, you are printing the memory address of y and z instead of the value like you did for the x value.
Use something like this.
#include <stdio.h>
typedef struct number
{
int x;
int y;
int z;
unsigned int final;
} number;
int main()
{
number numbers;
scanf("%d %d %d", &numbers.x, &numbers.y, &numbers.z);
printf("input: %d,%d,%d\n",numbers.x, numbers.y, numbers.z);
numbers.final=(numbers.x)*4000 + (numbers.y)*50 + (numbers.z);
printf("final: %d\n",numbers.final);
return 0;
}
Right and if you used malloc it looks like this.
#include <stdio.h>
#include <stdlib.h>
typedef struct number
{
int x;
int y;
int z;
unsigned int final;
} number;
int main()
{
number *numbers = malloc(1 * sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, numbers->y, numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d\n",numbers->final);
free(numbers);
return 0;
}
Running example here
The reason for your wrong answer is because you have kept the datatype as int which has max value of 32767 change it to unsigned long int as your ans calculates to 2400612