I am trying to 4x4 input in an 5x5 array and get the sum of each lines on the fifth lines.
I'm sure if you read my code below, you'll know what I am trying to talk about.
For example if I type in:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The expected Result should be:
1 2 3 4 10
5 6 7 8 26
9 10 11 12 42
13 14 15 16 58
28 32 36 40 136
Instead, I am getting a result like:
1 2 3 4 10
5 6 7 8 32792
9 10 11 12 42
13 14 15 16 58
28 32 36 40 -501277720
I thought about why I get these random values, but couldn't find a solution. Why am I getting these values and what can I do to solve it?
#include <stdio.h>
int main ()
{
int gradeArr[5][5];
int i,j;
printf("Input grades:\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&gradeArr[i][j]);
gradeArr[i][4] += gradeArr[i][j];
}
}
printf("%d\n", gradeArr[1][4]);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
gradeArr[4][i] += gradeArr[j][i];
}
gradeArr[4][4] += gradeArr[4][i];
}
printf("Result: \n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d ",gradeArr[i][j]);
}
printf("\n");
}
return 0;
}
You need to initialize the array.
Try
int gradeArr[5][5] = {0};
Related
I am trying to make a program that takes 3 inputs from the user: row/columns count and number range start and number range end.
In my case and for example lets say 4 1 16 so it means 4 rows and columns print numbers from 1-16.
I have a problem accomplishing this.
#include <stdio.h>
#include <stdlib.h>
int num1,num2,count,i,y;
int main()
{
count = 4;
num1 = 1;
num2 = 16;
for(i=1; i<=count; i++){
for(y=num1; y<=num2; y++){
printf("%d ",y);
}
printf("\n");
}
return 0;
}
The output is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Whereas I want my output to be:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
for(y=1; y<=count; y++){
printf("%d ",num1++);
}
That way you will ensure that all the numbers will be printed. you can use num1 variable that you have declared and initialized with 1.
Don't use unnecessary global variables. And keep the declarations close to where you use them. That way you won't have to look in the top of the page to check the type or if you initialized or not.
for(i=1; i<=count; i++){
printf("%d ",i);
if( i % 4 == 0)
printf("\n");
}
Print 4*(i-1)+y instead of y:
printf("%d ",4*(i-1)+y);
I am writing a program that takes the 1st day of a month and number of days in a month and prints the relevant calendar.
If I enter 3 and 30 it should print
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
This is the code I wrote
#include <stdio.h>
void printclndr();
int main(){
int first,days;
first = firstday();
if(first<1||first>7)
{
printf("%d is not a valid day. Try again. \n",first);
firstday();
}
days = monthdays();
if(days<28||days>31)
{
printf("Retry. Enter days for a valid month. \n");
monthdays();
}
printf("\n");
printclndr(first,days);
printf("\n\n");
main();
}
void printclndr(int day1, int numdays){
int date,check,count,tab;
check = day1 + numdays;
tab = 0;
for(count=1;count<check;count++)
{
if(count<day1)
{
printf("\t");
tab+=1;
}
if(count>=day1)
{
printf("%d\t",count-day1+1);
tab+=1;
}
if(tab==7)
{
printf("\n");
tab=0;
}
}
}
int firstday(){ //function to get the starting day of the month
int day; //initializing local variables
printf("First day of the month (1= Sun, 7= Sat): ");
scanf("%d",&day);
return day; //returns the value of day
}
int monthdays(){ //function to get no of days in a month
int days; //local variables
printf("Number of days in the month: ");
scanf("%d",&days);
return days;
}
firstday and monthdays scanf user inputs and are working fine. My code works and prints the calender. Now I want to rightalign my calender.
I want
1
8
15
instead of
1
8
15
How do I do it?
When you are printing the calendar use printf("%nd",number); n is a maximum number of decimal places, in your case n=2, printf("%2d\t",count-day1+1);;
This right-justifies the numbers as requested.
#include <stdio.h>
void printclndr(int day1, int numdays){
int day, count;
for(count = 1; count < day1-1; count++) { // initial padding
printf("\t");
}
count = day1 - 1;
for(day = 1; day <= numdays; day++) {
if(count >= 7) {
printf("\n");
count = 0;
}
else if(count > 0) {
printf("\t");
}
printf("%2d", day);
count++;
}
printf("\n");
}
int main(){
printclndr(3, 30);
printf("\n");
printclndr(1, 28);
printf("\n");
printclndr(2, 28);
return 0;
}
Program output:
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28
I used "%2d\t" and got the center aligned result.
Then I tried ("%*d/t",2,count-day1+1) and got the same result.
Then I downloaded Eclipse on which with some difficulty ran all the codes including #WeatherVane 's and voila!! I was using CodeLite in my Mac.
I don't get why sum == 105. For me it should be 100 but I can't get it to work that way. Can someone please explain?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum=0;
int t=0;
do{
t++;
sum= sum + t;
}
while(sum<100);
printf("sum:%d\n", sum); //prints 105
printf("t = %d\n", t);
return 0;
}
t starts at 0 and is incremented in each iteration of the loop.
sum starts at 0 and is increased by the value t in each iteration of the loop, until it is no longer smaller than 100. If we trace their values throughout the execution:
Iteration t sum
0 0 0
1 1 1
2 2 3
3 3 6
4 4 10
5 5 15
6 6 21
7 7 28
8 8 36
9 9 45
10 10 55
11 11 66
12 12 78
13 13 91
14 14 105
As you can see, 105 is the first value that sum gets that is not smaller than 100.
In your code: sum= 1+2+3+4+5+6+7+8+9+10+11+12+13+14 = 105.
Recently, I encountered with a problem that asked me to write a dynamic code that print n x n matrix in a zigzag pattern. Please help me with the code to get the output stated below.
Output:
rows: 5
cols: 5
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
The Code that I've tried so far is in static:
#include <stdio.h>
int main(){
int arr[3][3]={1,2,3,
4,5,6,
7,8,9};
int i, j, k;
for(i=0; i<3; i++){
printf("%d",arr[0][i]);
}
printf("\n");
for(j=2; j>=0; j--){
printf("%d",arr[1][j]);
}
printf("\n");
for(k=0; k<3; k++){
printf("%d",arr[2][k]);
}
printf("\n");
return 0;
}
Now I want the same thing to be done with the user stating rows and columns of an array..
This should work for you:
#include <stdio.h>
int main() {
int rows, columns;
int rowCount, columnCount, count = 0;
printf("Please enter rows and columns:\n>");
scanf("%d %d", &rows, &columns);
for(rowCount = 0; rowCount < rows; rowCount++) {
for(columnCount = 1; columnCount <= columns; columnCount++) {
if(count % 2 == 0)
printf("%4d " , (columnCount+(rowCount*columns)));
else
printf("%4d " , ((rowCount+1)*columns)-columnCount+1);
}
count++;
printf("\n");
}
return 0;
}
Input:
5 5
Output:
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
Simple dynamic logic with k variable
#include <stdio.h>
int main() {
int i,j,k=1,row,col;
printf("Enter row and col \n>");
scanf("%d %d", &row, &col);
for (i = 1; i <=row; i++)
{
for (j = 1; j <=col; j++)
{
if(i%2==0) k--;
printf("%4d",k); // it have to be in center of both condition
if(i%2!=0) k++;
}
k=k+col;
printf("\n");
}
return 0;
}
Input :
7 7
Output :
1 2 3 4 5 6 7
14 13 12 11 10 9 8
15 16 17 18 19 20 21
28 27 26 25 24 23 22
29 30 31 32 33 34 35
42 41 40 39 38 37 36
43 44 45 46 47 48 49
Why am I getting a segmentation fault with this code?
/* solver.h header file */
10 struct options{
11 unsigned int one:1, two:1, three:1, four:1, five:1, six:1, seven:1, eight:1, nine:1;
12 };
13
14
15 /* structure to describe a cell */
16 struct cell{
17 short value;
18 struct options open_options;
19 };
solver.c:
5 #include <stdio.h>
6 #include "solver.h"
7
8
9
10
11
12 int main(){
13 struct cell board [9][9];
14 int i=0,j=0;
15
16
17 for(i = 1; i<10; i++)
18 for(j = 1; j<10; j++)
19 (board[i][j]).value = j;
20
21 for(i = 1; i<10; i++){
22 for(j = 1; j<10; j++)
23 printf(" %d",(board[i][j]).value);
24 printf("\n");
25 }
26 return 0;
27 }
output:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Segmentation fault: 11
Arrays are indexed from 0, so the loops should be for(i = 0; i<9; i++) and not for(i = 1; i<10; i++)
In your case, you probably override part of the stack, but in general, going out of boundaries results in undefined behavior.
some_type array[9]; defines array to be an array of 9 elements, with subscripts going from 0 to 8 inclusive. You can't use array[9].
board[9][9] will contain elements with indices in the range 0...8, not 1...9. When you assigned to board[9][whatever], you actually overwrote memory that didn't belong to you, and this happened to cause the program to explode when return 0 handed control back to the C runtime and it started walking its structures to perform shut down.