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.
Related
I am trying to implement a set ADT using dynamic arrays. I have a set for odd and even numbers. When a array is full I use realloc to get a bigger array.
The problem is that this also seems to fill the array with unwanted numbers.
struct set
{
void **array;
int numitems;
int maxitems;
cmpfunc_t cmpfunc;
};
.
void set_add(set_t *set, void *elem)
{
if (!set_contains(set, elem))
{
if (set->numitems + 1 >= set->maxitems) // Make new bigger array if full
{
void **new_array = realloc(set->array, sizeof(void *) * set->maxitems * 2);
if (new_array == NULL)
printf("Error");
set->maxitems *= 2;
set->array = new_array;
}
set->array[set->numitems] = elem;
set->numitems++;
}
}
In main i use this to add numbers.
for (i = 0; i <= n; i++) {
if (i % 2 == 0)
set_add(evens, numbers[i]);
else
{
printset("Odd numbers":, odds);
set_add(odds, numbers[i]);
}
}
This is the output I get.
Output:
Odd numbers: 1
Odd numbers: 1 3
Odd numbers: 1 3 5
...
Odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
Odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31
Odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 18 19 20 21 22 23 24 25 26 27 28 29 30
...
After 31 is added, the array maxsize (=16) is doubled. Any ideas what causes the rest of the array to be filled? This is just a small part of the code, so if nothing here seems to be the cause I can post more.
=== Addition info: ===
static void printset(char *prefix, set_t *set)
{
set_iter_t *it;
printf("%s", prefix);
it = set_createiter(set);
while (set_hasnext(it)) {
int *p = set_next(it);
printf(" %d", *p);
}
printf("\n");
set_destroyiter(it);
}
.
set_iter_t *set_createiter(set_t *set)
{
set_iter_t *iter = malloc(sizeof(set_iter_t));
if (iter == NULL)
return NULL;
bobsort(set);
iter->set = set;
iter->cur = 0;
return iter;
}
int set_hasnext(set_iter_t *iter)
{
if (iter->set->array[iter->cur] == NULL)
return 0;
else
return 1;
}
void *set_next(set_iter_t *iter)
{
if (iter->set->array[iter->cur] == NULL)
return NULL;
else
{
void *elem = iter->set->array[iter->cur];
iter->cur++;
return elem;
}
}
It's for an assignment, so I'm following the function signatures. I'm used to make adt list with linked list and not arrays.
In function set_add you should change the if condition:
if (set->numitems + 1 >= set->maxitems) // Make new bigger array if full
to
if (set->numitems >= set->maxitems) // Make new bigger array if full
Let's look at the definition of realloc(): The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized.
So I believe you shouldn't use if (iter->set->array[iter->cur] == NULL) in set_hasnext(set_iter_t *iter) as you didn't initialize the re-allocate array to NULL. In another word, the value in the array may be random.
The 2 two changes that fix my issue:
In add_set(), change
if (set->numitems + 1 >= set->maxitems)
to
if (set->numitems >= set->maxitems)
And in set_hasnext(), change
if (iter->set->array[iter->cur] == NULL)
to
if (iter->cur >= iter->set->numitems)
Thanks to Tran and Anthony.
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};
I am writing 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:
The Code that I've tried so far with help of Rizier123 is in horizontal zigzag pattern:
#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
I want the same zigzag pattern output but vertically..
EDIT
Expected output:
1 10 11 20 21 30
2 9 12 19 22 29
3 8 13 18 23 28
4 7 14 17 24 27
5 6 15 16 25 26
This should work for you:
#include <stdio.h>
int main() {
int rows, columns;
int rowCount, columnCount;
printf("Please enter rows and columns:\n>");
scanf("%d %d", &rows, &columns);
for(rowCount = 0; rowCount < rows; rowCount++) {
for(columnCount = 0; columnCount < columns; columnCount++) {
if(columnCount % 2 == 0)
printf("%4d " , rows*(columnCount)+rowCount+1);
else
printf("%4d " , (rows*(columnCount+1))-rowCount);
}
printf("\n");
}
return 0;
}
Input:
5 5
Output:
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
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
an array is entered:
ary[] = [11 12 13 14 15 16 17 18 19 20 11 12 13 14 15 16 17 18 19 20 11 12 13 14 15 16 17 18 19 20 11 12 13 14 -1] (the negative one signifies that it is the end of the array)
a size is entered:
size = 34
I need this array to be rearranged so that it is in sequential order. Below is my code:
int x;
int numpasses;
int temp;
for(numpasses = 1; numpasses < size; numpasses++)
{
for(x = 0; x < size - numpasses; x++)
{
if(ary[x] > ary[x + 1] && ary[x] != ary[size - 1])
{
temp = ary[x];
ary[x] = ary[x + 1]; //THIS PORTION SWITHCHES TWO ADJECENT VALUES TO MAKE ARRAY SEQUENTIAL
ary[x + 1] = temp;
}
}
int a;
for(a = 0; a < size; a++)
{
printf("%d ", ary[a]);
}
}
This prints the numbers as follows:
11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 11 12 13 14 15 16 17 18 19 20 11 12 13 14
Replacing the line
if(ary[x] > ary[x + 1] && ary[x] != ary[size - 1])
by
if(ary[x] > ary[x + 1] )
worked fine for me.
I'll let you figure out why.
PS.
Please add
printf("\n");
after printing the array. It makes seeing the result at the end of the outer loop a little bit clearer.