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));
Related
When running this program using pointers and arrays to calculate the grade point average from the user input, it outputs a garbage value. How can I alter the code so that the output is correct?
void Insert_Grades(int *array)
{
int grades[4];
int i;
for (int i = 0; i < 4; i++)
{
printf("Enter grade %d: ", i + 1);
scanf("%d", &grades[i]);
}
array = grades;
}
void Calculate_Avg(int *array)
{
int i;
float avg;
float sum = 0;
for (i = 0; i < 4; i++)
{
sum += *(array + i);
}
avg = sum / 4;
printf( "Grade point average is: %f ", avg);
}
int main()
{
int grades[4];
int i;
printf("Enter the number of grades:\n");
Insert_Grades(grades);
Calculate_Avg(grades);
printf("\n");
return 0;
}
you cant assign arrays.
This operation assigns local pointer array with reference of the local array grades. For the extral world this operation means nothing.
array = grades;
You need to copy values instead.
memcpy(array, grades, sizeof(grades));
or
for (size_t index = 0; index < 4; index++)
array[index] = grades[index];
There are multiple problem in your code:
in function Insert_Grades, value are read into the local array grades. The last instruction array = grades has no effect because it only modifies the argument value, which is just local variable, a pointer to int that now points to the first element of grade array.
This explains why the program outputs garbage because the array grades defined in the main() function is uninitialized and is not modified by Insert_Grades().
You could copy the array grade to the caller array pointed to by array, but it seems much simpler to use the array pointer to read the values directly where they belong.
the variable i is defined multiple times, with nested scopes.
you should test the return value of scanf() to detect invalid or missing input.
Here is a modified version:
#include <stdio.h>
void Insert_Grades(int *array, int count) {
for (int i = 0; i < count; i++) {
printf("Enter grade %d: ", i + 1);
if (scanf("%d", &array[i]) != 1) {
fprintf(stderr, "invalid input\n");
exit(1);
}
}
}
float Calculate_Avg(const int *array, int count) {
float sum = 0;
for (int i = 0; i < count; i++) {
sum += array[i];
}
return sum / count;
}
int main() {
int grades[4];
int count = sizeof(grades) / sizeof(*grades);
float avg;
printf("Enter the grades:\n");
Insert_Grades(grades, count);
avg = Calculate_Avg(grades, count);
printf("Grade point average is: %.3f\n", avg);
return 0;
}
#include<stdio.h>
void printarr( int arr , int a,int b){
for(int z=0;z<a;z++){
for(int x=0;x<b;x++){
printf("the marks of student %d in subject %d and %d is :%d\n",z+1 ,
x+1 , x+2 , arr);
}
}
}
int main(){
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i=0; i<n_students; i++){
for (int j=0; j<n_sub; j++){
printf("enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d", marks[i][j]);
}
}
printarr(marks , 5 , 2);
return 0;
i am getting to put only two times and the outer loop is not repeating itself
please explain me in simple terms , i am just learning this launguage
complete begineer.
1.When passing two-dimensional arrays, it is not mandatory to specify the number of rows in the array. However, the number of columns should always be specified.
void printarr( int arr , int a,int b){ --->void printarr( int arr [][2], int
a,int b){
2.When reading array element in scanf you have missed &
scanf("%d", marks[i][j]);--->scanf("%d", &marks[i][j]);
3.When Printing array elements in printf you have to specify the index.
arr ---> arr[i][j]
#include<stdio.h>
void printarr( int arr [][2], int a,int b){
for(int z=0;z<a;z++){
for(int x=0;x<b;x++){
printf("the marks of student %d in subject %d is :%d\n",z+1,x+1,arr[z][x]);
}
}
}
int main(){
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i=0; i<n_students; i++){
for (int j=0; j<n_sub; j++){
printf("enter the marks of student %d in subject %d\n", i+1, j+1);
scanf("%d", &marks[i][j]);
}
}
printarr(marks, 5 , 2);
return 0;
}
You want this: (explanation in comments)
#include <stdio.h>
#include<stdio.h>
void printarr(int a, int b, int arr[a][b]) { // pass an array, not an int,
// and a and b must be before arr
for (int z = 0; z < a; z++) {
for (int x = 0; x < b; x++) {
printf("the marks of student %d in subject %d and %d is :%d\n", z + 1,
x + 1, x + 2, arr[z][x]); // use arr[x][z] here. arr obviously dons' make sense
}
}
}
int main() {
int n_students = 5;
int n_sub = 2;
int marks[5][2];
for (int i = 0; i < n_students; i++) {
for (int j = 0; j < n_sub; j++) {
printf("enter the marks of student %d in subject %d\n", i + 1, j + 1);
scanf("%d", &marks[i][j]); // you forgot the &
}
}
printarr(5, 2, marks); // you need to pass the size beforen the array
return 0; // read the documentation about VLAs
}
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;
}
}
I'm starting to learn programming in C, and I have this task where I have to write a program with part of the code on another file. But I'm having problems with that last part because I'm using matrices.
Here's the main body:
#include <stdio.h>
#include "otsenkatry.c"
int main()
{
int i, j;
int a[i];
int s, gru;
char A, B, C, D, E;
printf("Introduce the number os students ", &s);
fflush(stdout);
scanf("%d", &s);
printf("Introduce their grades\n");
fflush(stdout);
for (i = 0; i<s; i++)
{
printf("a[%d] = ", i);
fflush(stdout);
scanf("%d", &a[i]);
printf("Grade: %d %d \n", a, otsenkatry(a));
fflush(stdout);//}
}
return 0;
}
And that's the part with the problem:
int otsenkatry (int* a)
{
int i;
int gru;
if (a[i]<51)
{
gru=2;
}
if (a[i]>50 && a[i]<69)
{
gru=3;
}
if (a[i]>69 && a[i]<=85)
{
gru=4;
}
if (a[i]>85 && a[i]<=100)
{
gru=5;
}
return gru;
}
I figured, that it has to do with the pointers, but I don't know how to alter it.
Your matrix has undefined size:
int i, j;
int a[i];
To declare matrix a[] properly you need to pass the size - the value of i variable. Unfortunately, the i variable is declared one line above without initialization with any value.
There are a few problems with your code:
array a not properly declared
printing array a instead of integer
argument of otsenkatry is array, but should be an int
including a .c file
using undefined i value as array index in otsenkatry
the argument &s in the first printf is invalid
the otsenkatry function can be simplified
the variables j, gru, A, B, C, D, E are defined in main but never used
Here is a corrected implementation:
#include <stdio.h>
int otsenkatry (int v) {
if (v<51)
return 2;
if (v<69)
return 3;
if (v<=85)
return 4;
if (v<=100)
return 5;
return 0;
}
int main(){
int i, a[100], s;
printf("Introduce the number of students ");
fflush(stdout);
scanf("%d", &s);
if (s > 100)
s = 100;
printf("Introduce their grades\n");
fflush(stdout);
for (i = 0; i<s; i++) {
printf("a[%d] = ", i);
fflush(stdout);
scanf("%d", &a[i]);
printf("Grade %d: %d \n", a[i], otsenkatry(a[i]));
fflush(stdout);
}
return 0;
}
So here's my code:
#include <stdio.h>
double CalculateFinalScore(int assignment[], int midterm[], int finalExam[], double *scoreSum[]) {
int i = 0;
for (i = 0; i < 4; ++i) {
*scoreSum[i] = (assignment[i] * 0.2) + (midterm[i] * 0.3) + (finalExam[i] * 0.5);
}
}
int main(void) {
const int NUM_SCORES = 4; // Array size
double testScores[NUM_SCORES]; // User test scores
int i = 0;
double finalScore = 0.0;
int x[100];
int y[100];
int z[100];
double q;
// Prompt user to enter test scores
printf("Enter %d students test scores:\n", NUM_SCORES);
for (i = 0; i < NUM_SCORES; ++i) {
printf("Input student %d assignment score: ", (i+1));
scanf("%d", &(x[i]));
printf("Input student %d midterm score: ", (i+1));
scanf("%d", &(y[i]));
printf("Input student %d final exam score: ", (i+1));
scanf("%d", &(z[i]));
printf("\n");
}
printf("\n");
// Call function to calculate final score
CalculateFinalScore(x, y, z, &q);
for (i = 0; i < NUM_SCORES; ++i) {
printf("Final student test score: ");
printf("%lf\n", q);
}
return 0;
}
Basically, I want to have an output that display the final score for each student that I have inputed before. But the when my code tries to display the output its always segmentation fault. I tried to return the array before but it seems that it doesn't fix it. Can somebody help me?
Using your code (not tested)
#include <stdio.h>
#include <stdint.h>
#define NUM_SCORES 4
double CalculateFinalScore(int assignment[], int midterm[], int finalExam[], double scoreSum[], uint8_t num_scores)
{
int i = 0;
for (i = 0; i < num_scores; ++i)
{
scoreSum[i] = (assignment[i] * 0.2) + (midterm[i] * 0.3) + (finalExam[i] * 0.5);
}
}
int main(void)
{
double testScores[NUM_SCORES]; // User test scores
int i = 0;
int x[NUM_SCORES];
int y[NUM_SCORES];
int z[NUM_SCORES];
// Prompt user to enter test scores
printf("Enter %d students test scores:\n", NUM_SCORES);
for (i = 0; i < NUM_SCORES; ++i)
{
printf("Input student %d assignment score: ", (i+1));
scanf("%d", &(x[i]));
printf("Input student %d midterm score: ", (i+1));
scanf("%d", &(y[i]));
printf("Input student %d final exam score: ", (i+1));
scanf("%d", &(z[i]));
printf("\n");
}
printf("\n");
// Call function to calculate final score
CalculateFinalScore(x, y, z, testScores, NUM_SCORES);
for (i = 0; i < NUM_SCORES; ++i)
{
printf("Final student %d test score: ", )i+1));
printf("%lf\n", testScores[i]);
}
return 0;
}
You are passing a single double while your function need an array
Your function needs an array of double, not a pointer to an array of double