Creating a program that first takes in maximum amount of points for four assignments. Then it must take in seven students' scores for those four assignments. The program must then output each of the seven students score over the total available points. I can't figure out this pesky segmentation error. Program compiles and input of the maximum points available for the four assignments works just fine, the segmentation comes when I try to enter the students scores. Any help would be greatly appreciated!
#include<stdio.h>
int main (void)
{
int array[4][8];
int max, rows, cols, count;
printf("Please enter the maximum points available for the four assignment");
printf(" (add a space behind each and return when finished): \n");
scanf("%d %d %d %d", &array[0][0], &array[1][0], &array[2][0], &array[3][0]);
max=array[0][0]+array[1][0]+array[2][0]+array[3][0];
printf("Please enter each students set of scores");
printf(" (return after each individual score): \n");
for(cols=1; cols<8; cols++)
{
for(rows=0; rows<4; rows++)
{
scanf("%d", array[rows][cols]);
}
}
for(count=1; count<8; count++)
{
for(cols=1; cols<8; cols++)
{
printf("The points for student #%d, count");
printf(" (%d / %d)",array[0][cols]+array[1][cols]+array[2][cols]+array[3][cols], max);
printf("\n");
}
}
return 0;
}
One of your quotes is misplaced. Change
printf("The points for student #%d, count");
to
printf("The points for student #%d", count);
Related
Freshman studying computer science here, had this problem:
"Assume there is a matrix of dimensions mxn, representing the value of telephone calls of m subscribers in a period of n months. Write a C program which determines the months in which the telephone company registered the same value for all subscribers. The data transfer will be done exclusively through parameters."
This is what I tried and got graded a 5/10 on it. Could anyone tell me what's wrong with the code or provide a better solution?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,k,n,m,vectorfound[100],p,foundmonth;
printf("Number of months: ");scanf("%d",&m);
printf("Number of customers: ");scanf("%d",&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]= ",i,j);scanf("%d",&a[i][j]);
}
printf("You have chosen the matrix: "); printf ("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]); printf("\n");
}
for(i=0;i<m;i++)
{
p=0; foundmonth=1; k=a[i][0];
for(j=1;j<n;j++)
if (a[i][j]!=k) foundmonth=0;
if (foundmonth) // only executes if a month has been found
{
printf("The months in which the company has recorded the same value for all of the customers are: ");
vectorfound[p]=i; // vector which saves the months with the constant value
printf("%d ",vectorfound[p]);
p++;
}
}
getchar();
}
First statements in main
int a[3][3],i,j,k,n,m,vectorfound[100],p,foundmonth;
printf("Number of months: ");scanf("%d",&m);
printf("Number of customers: ");scanf("%d",&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]= ",i,j);scanf("%d",&a[i][j]);
}
The moment m or n is greater than 3, you are in undefined behavior territory with a strong likelihood of crashing. Your a array isn't big enough to hold anything bigger than a 3x3.
Also, your printf statement inside the for-lopp is preceding the scanf statement. Whatever gets printed is likely garbage data off the stack.
(QW) Write a well documented modular C program to compute SGPA of 2 students of particular semester containing 3 courses, suitably using structures.
Getting garbage value as output scanning of elements goes perfectly but when it comes to printing it displays random number as output, Is the sgpa calculator part of the code correct?
//reaplced courses with 3 and no of students with 2
#include<stdio.h>
struct studentlist
{
int Rno;
char name[20];
int course_marks[8];
}S[2];
int main()
{
int sgpa[2];
printf("ENTER DETAILS\n");
for(int i=0;i<2;i++)
{
printf("enter details of student %d\n",i);
scanf("%d %s",&S[i].Rno,S[i].name);
for(int j=0;j<3;j++)
{
printf("enter marks coursenumber %d \n",j);
scanf("%d",S[i].course_marks[j]);
}
}
//printing details
for(int i=0;i<2;i++)
{
printf("details of student %d are \n",i);
printf("%d %s",S[i].Rno,S[i].name);
for(int j=0;j<3;j++)
{
printf("coursewise marks for studenet %d are\n",j);
printf("%d\n",S[i].course_marks[j]);
}
}
printf(" \n\n ");
//sgpa calculation
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
sgpa[j]=0;
sgpa[j]+=S[i].course_marks;
}
}
//printing
for(int i=0;i<2;i++)
{
printf("%d",sgpa[i]);
}
}
Your usage of S[j].course_marks is wrong.
It is an array and it will be converted to a pointer in expressions here.
It seems the lines
scanf("%d",S[j].course_marks);
printf("%d\n",S[i].course_marks);
sgpa[j]+=S[i].course_marks;
should be
scanf("%d",&S[i].course_marks[j]);
printf("%d\n",S[i].course_marks[j]);
sgpa[j]+=S[i].course_marks[j];
Also the loop
for(int j=0;j<3;j++)
{
sgpa[j]=0;
sgpa[j]+=S[i].course_marks;
}
is bad because it accesses spga[2] while spga has only 2 elements spga[0] and spga[1].
Also it looks weird even if it weren't out-of-range access and
sgpa[j]=0;
sgpa[j]+=S[i].course_marks;
is equivalent to
sgpa[j]=S[i].course_marks;
You should rethink the algorithm here.
I've made a program that asks how many elements should be input in an array so if the user inputs 3, it will ask for a value to store in thearray, and then the program asks for input again, "input a number".
If the user inputs a value that is already stored in thearray, it should display for example "the number you input is stored already in array".
My professor gives this task to us, and he said that we should only be using array and for loop. I'm new to this computer programming currently studying Programming 1st year college. It would be great if someone could help me.
int main()
{
int Array[50], i, Number;
int cont;
printf("\nPlease Enter Number of elements in an array : ");
scanf("%d", &Number);
printf("\nPlease Enter %d elements of an Array \n", Number);
for (i = 0; i < Number; i++)
{
scanf("%d", &Array[i]);
}
printf("ENTER NUMBER: ");
scanf("%d", &cont);
if(cont = Array[0]){
printf("the number you input is stored in array# %d", Array[1]);
}
}
the expected output should belike this
Please Enter Number of elements in an array: 3
Please Enter 3 elements of an Array
10
20
30
ENTER NUMBER:20
the number you input is stored in array# 1
But the output in my code is.
Please Enter Number of elements in an array: 3
Please Enter 3 elements of an Array
10
20
30
ENTER NUMBER:20
the number you input is stored in array# 20
You have a common bug in your program here:
if(cont = Array[0]){
printf("the number you input is stored in array# %d", Array[1]);
}
you are overwriting cont with Array[0]. What you meant to say is:
if(cont == Array[0]){
printf("the number you input is stored in array# %d", Array[1]);
}
Also, you shouldn't only check if the number (cont) matches the first entry in the array, but all entries, like such:
for (int i = 0; i < Number; ++i) {
if(cont == Array[i]){
printf("the number you input is stored in array# %d", i);
break;
}
}
If the number is not found nothing is printed.
I've been trying to convert my python to C code and it's my first time using C.
Basically I want it to ask how many grades I will input and then put them in an array. The problem is that the amount keeps putting in the array. I thought that I could declare the size of the array after asking how many grades I was inputting but I think that's the problem. I am not sure how else to do it. I have a lot of printfs I was using for debugging.
Any Suggestions?
double enter_quiz_grades()
{
int quiz_amount,loop,i;
printf("Enter number of quiz grades to enter:");
scanf(" %d \n", &quiz_amount);
printf("You typed %d students\n",quiz_amount);
double temp=0;
double grades[quiz_amount];
for (loop = 0; loop<quiz_amount;loop++)
{
printf("loop is %d", loop);
i = loop+1;
printf("Enter grade for quiz %d: ",i);
scanf("%lf\n", &temp);
grades[loop] = temp;
printf("%lf",grades[loop]);
}
return 0.0;
}
The problem is you are using '\n' inside scanf. Also you can get rid of additional variables.
Here is the modified code:
double enter_quiz_grades() {
int quiz_amount, i;
printf("Enter number of quiz grades to enter:");
scanf("%d", &quiz_amount);
printf("You typed %d students\n",quiz_amount);
double temp = 0;
double grades[quiz_amount];
for (i = 0; i < quiz_amount; i++)
{
printf("loop is %d\n", i);
printf("Enter grade for quiz %d: ", i + 1);
scanf("%lf", &grades[i]);
printf("%lf\n",grades[i]);
}
return 0.0;
}
Just a small detail, not strictly related to the original question:
double grades[quiz_amount];
This creates an array, sized at runtime, from a variable. If this variable is read from somewhere, this can lead to a malicious user giving it a very high (or ngative!) value, and you are in big problems. The linux kernel is removing them from the source for this exact reason (as this syntax creates the array in the stack - attacker can modify the return address). Make this a dynamic array allocated on the heap (malloc()).
https://www.phoronix.com/scan.php?page=news_item&px=Linux-Kills-The-VLA
Hi I have to create a database that stores students number, name and also stores an array of course marks (1-N) in C Programming Language.
Everything worked until I started coding for the array of course marks. Then every time I compiled the code it kept crashing as soon as it asked to input the course marks.
Can you please tell me where I'm going wrong in my programming for this task? I have attached it to this message.
The program worked for inputting the name, student number, however I could not get the program to input array of marks. I have asked how many course marks to be entered and then used a for loop within the "void insert(void)" function to keep inputting the course marks into the array *marks. I am referring specifically to lines 24 to 30 in my programming code.
Always at this point the program kept crashing and I could not proceed further to enter more names or print the stored student details.
I think there is a problem with this part:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n");
scanf("%d", &(list[num_students].marks[num_marks]));
}
Anyway here is the full code:
#include <stdio.h>
#include <string.h>
struct student{
int number;
char name[10];
int marks[5];
};
struct student list[10];
int num_students = 0;
int num_marks = 0;
int *p;
void insert(void)
{
int student_number;
int i;
printf("Enter number: \n");
scanf("%d", &list[num_students].number);
printf("Enter NAME: \n");
scanf("%s", &list[num_students].name);
printf("Enter NO of courses: \n");
scanf("%d", num_marks);
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
num_students++; // HOW DO WE INPUT ARRAY MARKS??? MARK1: , MARK2: , MARK3 ,
}
void printtest(void)
{
int i;
for (i=0; i < num_students; i++)
{
printf("Name: \n");
puts(list[i].name);
printf("Number: %d \n", list[i].number);
printf("Mark: %d /100 \n", list[i].marks);
printf("\n");
}
}
int main(void)
{
int code;
int opt1;
int courses, i, k, j, counter;
for (;;){
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) DISPLAY REPORT OF ALL STUDENTS: \n");
scanf(" %d", &code);
switch (code){
case 1 :
insert();
break;
case 2 :
printtest();
break;
default:
printf("Illegal code\n");
printf("\n");
}
}
}
Apart from what others pointed out, I'd like to draw your attention towards the following:
void insert(void)
{
int student_number;
int i;
printf("Enter number: \n");
scanf("%d", &list[num_students].number);
printf("Enter NAME: \n");
scanf("%s", &list[num_students].name);
printf("Enter NO of courses: \n");
scanf("%d", num_marks);
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
num_students++;
}
void printtest(void)
{
int i;
for (i=0; i < num_students; i++)
{
printf("Name: \n");
puts(list[i].name);
printf("Number: %d \n", list[i].number);
printf("Mark: %d /100 \n", list[i].marks);
printf("\n");
}
}
There are three problematic statements:
scanf("%s", &list[num_students].name); This is why beginners should use compilers with all warnings enabled.
printf("Mark: %d /100 \n", list[i].marks); What did you declare marks as in the first place?
scanf("%d", num_marks); Seems like you put & operator where not needed and ignore where needed. Read your textbook before asking a question next time.
Seems like you're having a tough time understanding the concept of arrays and pointers. Read your text book thoroughly before venturing into the world of pointers. If you don't use them correctly, even compiler can't help you.
Also, even if I don't expect your program to have a robust input mechanism, at least array bounds checking is expected. Learn good habits from the beginning. They'll save a lot of your time while debugging later.
Seems to be an error in:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
The crash is probably because num_marks as index indexes beyond the array. Change to:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[i]));
}