Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the following code, which is not how to implement in C, (if it has one, the keyword with.On the other hand because the program does not access the data type structure, it should not give an error because the variables partial_n2, final_n2, name2 are defined in the struct.
The program has to store in an array of records the names of the students, their partial and final grades. Find the average grade and show a message of SUIT if the student exceeds or equals the grade of 5 or NOT SUIT if it is not enough. Do it for a number of 5 students.
#include <stdio.h>
#include <windows.h>
#include <conio.h>
//PROGRAM EJER009
#define numstudents 5
typedef struct notas{
char name2[20];
float partial_n2, final_n2;
}tnotas;
tnotas notas[numstudents];
tnotas clase;
char name[20];
float partial_n, final_n, n_media;
int i;
int main(){
for (i = 0; i <= numstudents;i++)
{
printf("Enter the student's name% d: ",i);
scanf("%s",name);
printf("Enter your partial note: ");
scanf("%f",&partial_n);
printf("Enter your final note: ");
scanf("%f",&final_n);
printf("\n");
with (clase[i])
{
partial_n2 = partial_n;
final_n2 = final_n;
name2 = name;
}
}
printf("cls");
printf("NAME\tPartial\tFinal\tMedia\tQUALIFICATION\n");
for (i = 1; i<=numstudents;i++){
with clase[i]
{
n_media = (partial_n2 + final_n2) / 2;
printf("%d %d %d",name2,partial_n2,final_n2);
system("color 14"); printf("%lf",n_media);
if (n_media >= 5)
{
system("color 11");
printf("SUITABLE :-)");
}
else
{
system("color 1");
printf("NOT SUITABLE :-(");
}
system("color 7");
}
}
getch();
return 0;
}
You can read a value of a member by:
float f;
f = notas[0].partial_n2;
You can write a value of a member by:
notas[0].partial_n2 = 10.3;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So i want my code actualy had different sum each cases, but the sum keep adding from the other cases, like for example the cases 2 sum are sum from loop 2nd + cases 1, the cases 4 sum are sum from cases 1,2,3 and loop number 4
the Output.
`
#include <stdio.h>
int main() {
int cases;
int day;
int animal;
int n;
int a;
int sum = 0;
animal = 0;
printf("Enter cases \n ");
scanf(" %d", &n);
for (cases = 0; cases < n; cases++)
{
printf("cases #%d\n", cases+1);
printf("Enter how many days.\n");
scanf(" %d", &a);
for(day=1;day<=a;){
printf("Enter how many animal that you capture at day #%d\n", day);
scanf(" %d", &animal);
day++;
sum = sum + animal;
}
animal = 0;
day = 1;
printf("cases#%d = %d\n", cases + 1, sum);
}
return 0;
}`
You need to return sum to 0 in every case
Put sum=0; under for (cases...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following code that works fine:
#include <stdio.h>
#include <stdlib.h>/*need this for rand()*/
#include "random.h"
#include <time.h>/*for time() function*/
int main()
{
int arr[5], a = 0;
printf("enter 5 array elements\n");
scanf("%d", &arr[0]);
scanf("%d", &arr[1]);
scanf("%d", &arr[2]);
scanf("%d", &arr[3]);
scanf("%d", &arr[4]); /*scan all elements*/
srand(time(NULL)); /*set the seed*/
a = arr[rand() % ARR_SIZE(arr)];/*from .h file*/
printf("%d\n",a);/*print the random element generated above*/
return 0;
}
It picks a random integer for an array of 5 integers.
I need the following modifications to it:
A function should accept two parameters — an array of void pointers and the array length. It should return a void pointer.
The function should pick an element from the array at random and return it.
int main() must seed the random number generator and then call the function. Then finally it should print the random element that was generated.
I don't know how to modify it to meet above requirements.
Here are the random.h file contents:
#define ARR_SIZE(arr) ( sizeof((arr)) / sizeof((arr[0])) )
This should do the trick.
void* getRandomElement(void** array, int size)
{
int* arr = (int*)*array;
return (void*)&(arr[rand() % size]);
}
int main(void)
{
int arr[5];
void* p = (void*)arr;
printf("enter 5 array elements\n");
scanf("%d", &arr[0]);
scanf("%d", &arr[1]);
scanf("%d", &arr[2]);
scanf("%d", &arr[3]);
scanf("%d", &arr[4]); /*scan all elements*/
srand(time(NULL)); /*set the seed*/
printf("random element %d\n", *(int*)getRandomElement(&p, ARR_SIZE(arr)));
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a file which includes product name, product quantity and product price in the following format.
file.txt
3
Product Qty Price
Tv 2 10
Mobile 3 20
Computer 5 30
I want to read the number of products from the integer (such as 3) given above the product list and count the total price of the products. The program will use struct to read the product details such as
struct product {
Char name[30];
int qty ;
float price;
}
What are the best practices to make this program easier?
Please try if the following program can help you.
#include <stdio.h>
#include <stdlib.h>
struct product {
char name[30];
int qty;
float price;
};
int main(void) {
int count = 0;
char line[100];
FILE *fptr;
fptr = fopen("file.txt", "r");
fscanf(fptr, "%d", &count); // count = 3
struct product *p = malloc(sizeof(struct product));
int i = 0;
double sum = 0;
while (i < count + 2 && fgets(line, sizeof(line), fptr) != NULL) {
if (i > 1) {
sscanf(line, "%s %d %f\n", (*p).name, &(*p).qty, &(*p).price);
sum = sum + (*p).price;
}
i++;
}
printf("sum: %f\n", sum);
free(p);
return 0;
}
Test
$ gcc main.c
$ ./a.out
sum: 60.000000
$
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
loops for structures
main()
{
structure perdata //MADE A STRUCTURE
{
char name;
int age;
float salary;
}p1,p2,p3;
int i;
for(i=1;i<4;i++)
{
printf("p%d.name",i);
scanf("%s",);/*should loop and read names of p1,p2,p3*/
}
printf("p1.name:%s",p1.name);
getch();
}
You should use array of structures so that you may iterate through it.
Example : An array of student structure,
#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
int main(){
struct student s[10]; //Ten student details maybe stored
int i;
printf("Enter information of students:\n");
//get the details of 10 students
for(i=0;i<10;++i)
{
s[i].roll=i+1;
printf("\nFor roll number %d\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
//display the details of 10 students
printf("Displaying information of students:\n\n");
for(i=0;i<10;++i)
{
printf("\nInformation for roll number %d:\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
return 0;
}
Use the above sample code & write your program.
Always make arrays of structures, classes etc. whenever you need to loop through instances.
int main(void)
{
struct perdata //MADE A STRUCTURE
{
char name[20];
int age;
float salary;
};
struct perdata p[4];
for(int i=0;i<4;i++)
{
//printf("p%d.name",i);
scanf("%s", &p[i].name);/*should loop and read names of p1,p2,p3*/
}
for(int i=0;i<4;i++)
{
printf("p%d.name:%s\n",i+1, p[i].name);
}
getch();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am extremely new to C I am trying to write a simple program that will prompt the user to enter a number of modules followed by credit and numeric grade for each module, this program must print out the information received by the user including the letter grade for each module and the GPA at the bottom. I have got to the point where I am trying to convert numeric grade to a letter grade and I am trying to write a class that I can input the numeric grade say "mod1.grade" that will return the letter grade but cannot work this out as I said I am a complete newby so would be gratefull of any help received.
#include <stdio.h>
#include <string.h>
struct module { char moduleid[10]; int credit; float grade; };
int main( ) {
struct module mod1,mod2; printf("Please enter: module id, module credit and module grade\n");
scanf("%s%d%f",mod1.moduleid,&mod1.credit,&mod1.grade);
scanf("%s%d%f",mod2.moduleid,&mod2.credit,&mod2.grade);
String getGrade (float input){
String letterGrade;
if(input >= 80&&<=100){
letterGrade = 'A';
}
return letterGrade;
}
printf( "Module id\tCredit\t\tGrade\n");
printf("%s\t\t%d\t\t%f\t%s\n",mod1.moduleid,mod1.credit,mod1.grade,getGrade(mod1.grade));
printf( "%s\t\t%d\t\t%f\n",mod2.moduleid,mod2.credit,mod2.grade);
return 0;
}
Your code should be:
#include <stdio.h>
#include <string.h>
struct module {
char moduleid[10];
int credit;
float grade;
};
char getGrade (float input){ //you need to return a char
//there isn't any "String" data type in C
char letterGrade; //char not String
if(input >= 80&&input<=100){ //note the difference here
letterGrade = 'A';
}
return letterGrade;
}
int main( ) {
struct module mod1,mod2;
printf("Please enter: module id, module credit and module grade\n");
scanf("%s%d%f",mod1.moduleid,&mod1.credit,&mod1.grade);
scanf("%s%d%f",mod2.moduleid,&mod2.credit,&mod2.grade);
printf( "Module id\tCredit\t\tGrade\n");
printf("%s\t\t%d\t\t%f\t%c\n",mod1.moduleid,mod1.credit,mod1.grade,getGrade(mod1.grade));//%c for a character
printf( "%s\t\t%d\t\t%f\t%c\n",mod2.moduleid,mod2.credit,mod2.grade,getGrade(mod2.grade)); //same here too
return 0;
}