printing a periodical series of a number in C - c

Given a Periodical Series of numbers
Example X=3
the Periodical Series of x=3 must look like this
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
Wanted: to write a program in C to print this series
and given : Assume maximum value of x can be 10
I tried to start with ideas.. but all failed..
Please HELP . thanks :)

def yourFunction(n, x):
recursiveFunction(n, x, 0, [0]*n)
def recursiveFunction(depth, breadth, currentDepth, indexes):
if currentDepth >= depth:
print indexes
else:
for indexes[currentDepth] in range(0, breadth):
recursiveFunction(depth, breadth, currentDepth + 1, indexes)

#include<stdio.h>
#include<stdlib.h>
int printSeries(int list[], int max, int level){
if(level == max){
int i;
for(i=0;i<max;++i)
printf("%d ", list[i]);
printf("\n");
return 1;
}
while(list[level]<=max){
list[level]+=printSeries(list, max, level+1);
}
list[level] = 1;
return 1;
}
void printSeriesStart(int x){
int i, *list = malloc(x*sizeof(int));
for(i=0;i<x;++i){
list[i]=1;
}
printSeries(list, x, 0);
free(list);
}
int main(void){
int X = 3;
printSeriesStart(X);
return 0;
}

Related

C - Larger Output Than I have in File (Size Problem)

I have a file, with format:
Course - Grade Count - Grades
Programming 10 3 4 5 4 3 2 4 5 2 3
Mathematics 8 3 3 4 5 3 2 2 3
Physics 6 3 4 5 3 4 5
Design 6 5 4 5 3 2 4
Logistics 8 3 4 5 3 1 1 2 4
Ex: Course - Programming, Grade Count - 10 and Grades - 3 4 5 4 3 2 4 5 2 3
I already have
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 70
int main(void)
{
char subject[SIZE];
int gradeCount;
int grades[SIZE];
FILE *fp = fopen("C:\\Project\\project.txt", "r"); //opening already created file
if (fp == NULL) {
perror("Error opening file");
return(-1);
}
for (int i = 0; i < SIZE; i++) {
fscanf(fp, "%s %d", &subject[i], &gradeCount);
printf("%s \n", &subject[i]);
//printf("%d \n", gradeCount);
for (int k = 0; k < gradeCount; k++)
{
fscanf(fp, "%d", &grades[k]);
// printf("%d \n" , grades[k]);
}
if (i == SIZE) {
break;
}
}
fclose(fp);
return 0;
}
I need to print out "Course", "Grade Count" and "Grades" without any problems, later on I need to make a search and so I need to separate them from each other, but that is not the case, now I will show you the outputs for all cases, when I output first "Subject/Course" then "Grade Count" and finally "Grades".
For Courses:
Programming
Mathematics
Physics
Design
Logistics
ogistics
gistics
istics
stics
tics
ics
cs
s
#
##
#
#
For Grade Count:
10
8
6
6
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
And for Grades:
3
4
5
4
3
2
4
5
2
3
3
3
4
5
3
2
2
3
3
4
5
3
4
5
5
4
5
3
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
3
4
5
3
1
1
2
4
In all cases, additional things are added to original stuff that should be printed out, I don't know where it comes from, I thought about pointers, but don't know much about them. Any suggestions?
Just need to print everything normally to normally search for everything (Courses, grade count and grades) later on.
You need to exit the loop early if it fails to read anything in. You can do that by checking the return value of fscanf. If the first call doesn't return 2, you know that it didn't read in 2 values and can break out of the loop.
You're also calling fscanf and printf incorrectly for dealing with a string. You are moving the starting point of where you read into/print from, which isn't needed and reduces the maximum space available to you.
Updated code looks something like this
for (int i = 0; i < SIZE; i++) {
if(fscanf(fp, "%s %d", subject, &gradeCount) != 2) {
break;
}
printf("%s ", subject);
//printf("%d \n", gradeCount);
for (int k = 0; k < gradeCount; k++)
{
fscanf(fp, "%d", &grades[k]);
// printf("%d" , grades[k]);
}
}

Improper Initialization of 2D Array in C

I am trying to construct a 2D array for an assignment. I've used a nested for loop to construct the 2D array using scanf():
int width;
int height;
scanf("%d %d",&width,&height);
int array[width][height];
for (int i=0;i<height;i++){
for (int j=0;j<width;j++){
scanf("%d",&array[i][j]);
}
}
However when I print the array, I can see that it has been constructed in a strange way, where all the numbers of the first line past a certain point are the first few numbers from the second line (instead of what they should be). The next lines after work fine.
Example:
Input:
6 2
1 3 5 7 9 1
2 4 6 8 0 2
3 4 2 0 1 3
The created array looks like this:
1 3 2 4 6 8 (<-- these last 4 numbers are the first 4 numbers of the second line)
2 4 6 8 0 2 (correct)
3 4 2 0 1 3 (correct)
Any ideas? Thanks a lot.
Your declaration of array
int array[width][height];
is wrong. The outer loop goes from 0 to height - 1, but array[i] can only go
from 0 to width - 1. The same applies for the inner loop. You swapped width
and height in the declaration of the array, it should be
int array[height][width];
Also note that for the matrix
1 3 5 7 9 1
2 4 6 8 0 2
3 4 2 0 1 3
the width is 6 and the height is 3, so the correct input should be
6 3
1 3 5 7 9 1
2 4 6 8 0 2
3 4 2 0 1 3
I compiled and run this code:
#include <stdio.h>
int main(void)
{
int width;
int height;
scanf("%d %d",&width,&height);
int array[height][width];
for (int i=0;i<height;i++){
for (int j=0;j<width;j++){
scanf("%d",&array[i][j]);
}
}
printf("----------------\n");
for (int i=0;i<height;i++){
for (int j=0;j<width;j++){
printf("%d ", array[i][j]);
}
printf("\n");
}
}
And the output is:
$ ./b
6 3
1 3 5 7 9 1
2 4 6 8 0 2
3 4 2 0 1 3
----------------
1 3 5 7 9 1
2 4 6 8 0 2
3 4 2 0 1 3
as you can see, now it's reading correctly. See https://ideone.com/OJjj0Y

Print a console "picture" using recursion

I'm having some trouble printing the following picture.
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 (16 times)
2 2 2 2 2 2 2 2 2 2 2 2 (12 times)
3 3 3 3 3 3 3 3 (8 times)
4 4 4 4 (4 times)
3 3 3 3 3 3 3 3 (8 times)
2 2 2 2 2 2 2 2 2 2 2 2 (12 times)
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 (16 times)
It's easy for me to implement an iterative algorithm, but I have to use recursion. I've written the following code (C++) that seems to do the job.
void print(int n, int current)
{
int offset = (n / 2) * (current - 1);
int i;
for (i = 0; i < offset; i++)
printf(" ");
for (i = 1; i <= (n - current + 1) * n; i++)
printf("%i ", current);
printf("\n");
}
void picture(int n, int current)
{
if (current < n) {
print(n, current);
picture(n, current + 1);
print(n, current);
}
else
if (current == n)
print(n, current);
}
int main()
{
int n;
input: printf("Enter n --> ");
scanf_s("%i", &n);
if ((n < 1) || (n > 9) || (n % 2 == 1)) {
printf("ERROR: n must be an even decimal digit!\n");
goto input;
}
picture(n, 1);
return 0;
}
I wonder whether there is a simpler way to write the recursive function here.
Update: I've tried to identify the recursion in a much simpler problem of printing the "pyramid":
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
The function pyram receives two parameters: the maximum number n (5 in our case) and the current number k. k is printed k times, then pyram is called with the parameters n and k + 1. This happens only when k <= n.
void pyram(int n, int k)
{
if (k <= n) {
for (int i = 1; i <= k; i++)
printf("%i ", k);
printf("\n");
pyram(n, k + 1);
}
}
I've written my solution to the original problem in a similar manner.
You can use static variables in the recursive function. In this case the function declaration will look simpler and you will not need an auxiliary function.
For example
#include <stdio.h>
void display_pattern( unsigned int n )
{
const unsigned int FACTOR = 4;
static unsigned int value = 1;
static int indent = 1;
if ( n )
{
printf( "%*u", indent, value );
for ( unsigned int i = 1; i < FACTOR * n; i++ ) printf( " %u", value );
putchar( '\n' );
indent += FACTOR;
++value;
display_pattern( --n );
indent -= FACTOR;
--value;
}
if ( n++ )
{
printf( "%*u", indent, value );
for ( unsigned int i = 1; i < FACTOR * n; i++ ) printf( " %u", value );
putchar( '\n' );
}
}
int main(void)
{
const unsigned int N = 10;
while ( 1 )
{
printf( "Enter a non-negative number less than %u (0 - exit): ", N );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < N ) ) n = N - 1;
putchar( '\n' );
display_pattern( n );
putchar( '\n' );
}
return 0;
}
The program output can look like
Enter a non-negative number less than 10 (0 - exit): 10
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7 7 7 7 7 7
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Enter a non-negative number less than 10 (0 - exit): 4
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4
3 3 3 3 3 3 3 3
2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Enter a non-negative number less than 10 (0 - exit): 0
As for the function pyram then it can look like
void display_triangle( unsigned int n )
{
if ( n )
{
display_triangle( n - 1 );
for ( unsigned int i = 0; i < n; i++ ) printf( "%u ", n );
putchar( '\n' );
}
}

Algorithm to fill 2D array in C

and sorry for the noob question.
So I've been asked to create an algorithm to fill a 2D array. They didn't say what the rules were, however, I've been told that the resulting matrix should look like this:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
I haven't been told if the matrix is necessarily square, but the size may vary.
So substantially what I'm seeing is the matrix is vertically, horizontally and diagonally symmetrical.
Now whatever I try, it ends up being super complicated, while as I look at it, I feel like it should be pretty simple...
Any trick or snippet on how you'd do it?
Thanks in advance.
You need 2 nested loops, to traverse through rows and columns. The content of the field is the minimum of the control variables and and the minimum of the difference of a control variable and the size of the array dimension, incremented by 1.
N = 5
0: min(0, N-0-1) + 1 = 1
1: min(1, N-1-1) + 1 = 2
2: min(2, N-2-1) + 1 = 3
3: min(3, N-3-1) + 1 = 2
4: min(4, N-4-1) + 1 = 1
#include <stdio.h>
#define N 5
#define MIN(a,b) (((a)<(b))?(a):(b))
int main()
{
int a[N][N];
for ( int i = 0; i < N; ++ i )
{
for ( int j = 0; j < N; ++ j)
{
int minI = MIN(i, N-i-1);
int minJ = MIN(j, N-j-1);
a[i][j] = MIN(minI, minJ) + 1;
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Output:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
See the live example

Debugging a function in C

This function takes an integer array, the number of elements
in the array and tries to find a majority element in the
array. If the majority element exists, it is placed in
*result and the function returns true.
If no majority element exists, then the function returns
false. In that case, *result should not be used.
My output isn't working correctly for the program I'm writing and it is because of this findMajority function I think.
This is what the output is supposed to look like: http://pastebin.com/Q5ycXHrg
This is what my output looks like: http://pastebin.com/7P1ZTpML
This is the input:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3
1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1
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
1 2 3
1 1 1
1 2 1
1 2
1 1
2
1 1 1 1 2 3 4 5 6 7
Here is the function:
int findMajority(int *array, int count, int *result){
int i, counter, bcount = 0, ccount = 0, candidate, j;
if(count == 1) {
*result = *array;
return true;
}
if(count % 2 != 0 ) {
for(i = 0; i < count; i++) {
if(*(array + i) == *(array + count)) {
counter++;
}
}
if(counter > (count/2)) {
*result = *(array + count);
return true;
}
else {
*(array + count) = 0;
count--;
}
}
for(j=0; j <= count; j += 2) {
if(*(array + j) == *(array + (j + 1))) {
*(array + (count + 1)) = *(array + j);
bcount++;//how many numbers on the end of the array
}
}
if(bcount == 1) {
int k = count;
while(*(array + k) == 0) {
candidate = *(array + k);
}
}
else
findMajority((array + count), count, result);
for(j=0; j <= count; j += 2) {
if(*(array + j) == candidate) {
ccount++;
}
}
if(ccount > (count/2)) {
*result = candidate;
return true;
}
else
return false;
}
Your function has a lot of problem.
Without intialising counter you are incrementing it
check whether array[count] is the valid last element or array[count-1] is the correct one
In this code for(j=0; j <= count; j += 2){
if(*(array + j) == *(array + (j + 1))){
*(array + (count + 1)) = *(array + j);
bcount++;//how many numbers on the end of the array
}}
for count= 3 you are accessing array[4] array[5] etc.
And this is a infinite loop. you are not modifying condition variable inside the loop while(*(array + k) == 0) { candidate = *(array + k); }
I suggest you learn how to use a debugger to debug your program. For example, after wrapping your code with the following:
#include <stdio.h>
typedef enum { false, true } boolean;
// ((( your function here )))
int main(int argc, char* argv[])
{
int result = 0;
int number = 0;
int test[] = { 1, 1, 1, 1, 1, 1, 1 };
result = findMajority(&test[0], sizeof(test) / sizeof(int), &number);
printf("Result = %d, Number = %d\n", result, number);
return 0;
}
Assuming you put this into 'question.c' you could then issue the commands (assuming you have gcc and gdb):
$ gcc -g -o question question.c
$ gdb ./question
(gdb) b findMajority
Breakpoint 1 at 0x80483ea: file question.c, line 6.
(gdb) run
Starting program: ./question
Breakpoint 1, findMajority (array=0xbffff4bc, count=7, result=0xbffff4d8) at question.c:6
6 int i, counter, bcount = 0, ccount = 0, candidate, j;
You can then use the n command to step to the next line, and the p command to print variables to see what's going wrong. For example, you could find some of the problems that Toms pointer out relatively quickly:
39 while(*(array + k) == 0){
(gdb) n
40 candidate = *(array + k);
(gdb) n
39 while(*(array + k) == 0){
(gdb) n
40 candidate = *(array + k);
(gdb) n
39 while(*(array + k) == 0){
(gdb) n
There's your infinite loop.
(gdb) p counter
$3 = -1207959944
And there's your uninitialized counter.
Part of learning programming is figuring out strategies of determining just what went wrong. Some people like to use text-based debuggers like gdb. Some people like graphical debuggers like you could find in Eclipse CDT. Some people put printf() statements throughout their code.
Once you're really good, like Toms, you can just read it and rattle off the problems. ;-)

Resources