OK. So I'm just trying to do some practice with simple algorithms and asterisks in C. This is not a homework problem. In my book there is a project that asks you to create a program that makes "these shapes" out of asterisks. One of them is a pyramid. While I can find numerous examples of how to make pyramid on the web, I can't find one that helps me figure out the algorithm for just the border of one. And I have not seen one on here either.
Now here is the code I have already.
#include <stdio.h>
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 1; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
for( j = 1; j <= 2*i-1; j++)
{
printf("*");
}
printf("\n");
}
for ( i = 6; i < 10; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
for( j = 1; j <= i+1-3*h; j++)
{
printf("*");
}
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
When you run the program it should, since I've ran it numerous times with no errors, give you this shape:
....*
...***
..*****
.*******
*********
.*******
..*****
...***
....*
I keep thinking that the loop that has the asterisk would probably need more loops to determine the location of the spacing between the asterisks. But I can't really begin to think of how to do this. What I want to end up with is this:
....*
...*.*
..*...*
.*.....*
*.......*
.*.....*
..*...*
...*.*
....*
Any help with this would be greatly appreciated.
I am a beginner programmer. In my first University level programming class. We're only learning C in this class. We've learned up to Multidimensional Arrays, and started learning functions on Thursday.
Here is a hint. You want the "first and last *" - so look at the loop
for( j = 1; j <= 2*i-1; j++)
{
printf("*");
}
And change it to something like:
printf("*"); // first *
for( j = 2; j<= 2*i-2; j++) printf(" "); // spaces until...
printf("*"); // last *
See how you get on with that… this is not the only place where you need to make this change.
EDIT in response to the request by #KalaJ I provide complete code - once with the first and last star, and once without. You can pick which one you want to use by changing the condition in the #if 0 preprocessor directive. If you set it to #if 1 you get the original pattern.
#include <stdio.h>
#if 0
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 1; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
printf("*");
for( j = 2; j <= 2*i-2; j++)
{
printf(" ");
}
if(i>1) printf("*");
printf("\n");
}
for ( i = 6; i < 10; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
printf("*");
for( j = 1; j <= i-3*h-1; j++)
{
printf(" ");
}
if(i<9) printf("*");
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
#else
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 2; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
printf("*");
for( j = 2; j <= 2*i-2; j++)
{
printf(" ");
}
if(i>1) printf("*");
printf("\n");
}
for ( i = 6; i < 9; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
printf("*");
for( j = 1; j <= i-3*h-1; j++)
{
printf(" ");
}
if(i<9) printf("*");
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
#endif
This produces either
* *
* *
* *
* *
* *
* *
* *
or
*
* *
* *
* *
* *
* *
* *
* *
*
Define each edge as a vector
find max(Y)
iterate from max(Y) down
for each Y find the matching X then print dots or star if you intersect a vector until max(X) on that line
Related
here below i have given multiplication of matrices in c language using for loop but can any help me make a more simplified version or can any help me make it using while loop
i want a simplified version
i want a code in while loop
:) just learning
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10][10], b[10][10], mul[10][10], r, c, i, j, k;
system("cls");
printf("enter the number of row=");
scanf("%d", &r);
printf("enter the number of column=");
scanf("%d", &c);
printf("enter the first matrix element=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("enter the second matrix element=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("multiply of the matrix=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
mul[i][j] = 0;
for (k = 0; k < c; k++)
{
mul[i][j] += a[i][k] * b[k][j];
}
}
}
//for printing result
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", mul[i][j]);
}
printf("\n");
}
return 0;
}
It will not make code simpler only harder to read.
One of the loops example:
printf("multiply of the matrix=\n");
i = 0;
while (i < r)
{
j = 0;
while(j < c)
{
mul[i][j] = 0;
k = 0;
while(k < c)
{
mul[i][j] += a[i][k] * b[k][j];
k++;
}
j++;
}
i++;
}
I'm asked to find the highest frequency from an array of elements and all elements with said frequency. My code seem to work just fine but it seems to have a mistake somewhere when i submit it. Can anyone help me find the error?
Format Input:
The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤i≤N) which indicate ith element in the array.
Format Output:
Consists of T lines where each line has the format “Case #X: Y ”, where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.
Constraints:
1 ≤ T ≤ 20 | 2 ≤ N ≤ 20.000 | 1 ≤ Xi ≤ 2 × 10^5
Sample Input:
3
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1
4
1 1 1 3
Sample Output:
Case #1: 2
1 2 5
Case #2: 2
1 2 5
Case #3: 3
1
Here is my code:
#include <stdio.h>
int main() {
int T, N[20];
scanf("%d", &T); getchar();
int A[T][20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N[i]); getchar();
for (int j = 0; j<N[i]; j++) {
scanf("%d", &A[i][j]); getchar();
}
int X = 0;
for (int j = 0; j<N[i]; j++) {
for (int k = j + 1; k<N[i]; k++) {
if (A[i][k]<A[i][j]) {
X = A[i][j];
A[i][j] = A[i][k];
A[i][k] = X;
}
}
}
}
int f[20000];
for (int i = 0; i<T; i++) {
int c = 0, mc = 0;
for (int j = 0; j<N[i]; j++) {
c = 1;
if(A[i][j] != -1) {
for (int k = j+1; k<N[i]; k++) {
if (A[i][j] == A[i][k]) {
c++;
A[i][k] = -1;
}
}
f[j]=c;
}
if (c>mc) {
mc = c;
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N[i]; j++) {
if (A[i][j] != -1) {
if (f[j] == mc) {
printf ("%d", A[i][j]);
if (j<N[i]-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
EDIT
So I made another code where instead of inputting all arrays at once and outputting everything at once, this code outputs the frequency and elements after i input the first arrays of numbers. But it seems like the code still have problems and i can't find where... P.s I'm pretty new to this, so i apologise for the lack of efficiency of my codes.
NEW CODE
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T); getchar();
int A[20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
for (int j = 0; j<N; j++) {
scanf("%d", &A[j]); getchar();
}
int X;
for (int j = 0; j<N; j++) {
for (int k = j + 1; k<N; k++) {
if (A[k]<A[j]) {
X = A[j];
A[j] = A[k];
A[k] = X;
}
}
}
int f[N], c = 0, mc = 0;
for (int j = 0; j<N; j++) {
c = 1;
if(A[j] != -1) {
for (int k = j+1; k<N; k++) {
if (A[j] == A[k]) {
c++;
A[k] = -1;
}
}
f[j]=c;
if (c>mc) {
mc = c;
}
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N; j++) {
if (A[j] != -1) {
if (f[j] == mc) {
printf ("%d", A[j]);
if (j<N-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
It took me a couple of days but i finally got how to do this. Apparently, it was not as complicated as i thought... here is the working code. Thanks to everyone who helped :)
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T);
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
//INPUT elements and counting frequncy for each element
int f[200001] = {0}, E = 0;
for (int j = 0; j<N; j++) {
scanf("%d", &E); getchar();
f[E]++;
}
//find max frequency and how many elements with max frequency
int max = 0, c = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
c ++;
}
if (f[j]>max) {
max = f[j];
c = 1;
}
}
//OUTPUT result
printf("Case #%d: %d\n", i+1, max);
int counter = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
counter ++;
if (counter<c){
printf("%d ", j);
} else {
printf("%d\n", j);
}
}
}
}
return 0;
}
Actually I did the easy part, but couldn't get more. I am a new Comp Eng student and we're trying to learn Loops. Our professor gave us this work to learn something. I did first part easily from last lesson. But I dont know how to lead it further.
I need to reflect this and get a full Bow Tie
Here it is my code:
#include <stdio.h>
main(); {
int sayi;
printf("Sayiyi gir > ");
scanf("%d",&sayi);
for (int i = 0; i < sayi; i++) {
for (int j = 0; j < i; j++) {
printf("*\t");
}
printf("\n");
}
for (int i = sayi; i >= 1; i--) {
for (int j = 0; j < i; j++) {
printf("*\t");
}
printf("\n");
}
return 0;
}
Change your code like:
for (i = 0; i <= sayi; i++) {
// Prints left part of the tie in row
for (j = 0; j < i; j++) {
printf("*\t");
}
// Prints the spaces between tie edges
for (j = 0; j < sayi - i; j++) {
printf("\t\t");
}
// Prints right part of the tie in row
for (j = 0; j < i; j++) {
printf("*\t");
}
printf("\n");
}
Repeat similar but reverse for lower part of the tie.
I'm trying to print out a hollow, open tent shape using asterisk stars "*". The code uses two for loops, the first for the rows, and the other for the columns.
following is my code:
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
} else {
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf(" ");
}
if(j == n) {
printf("*");
for(j = 1; j <= n; j++) {
printf(" ");
}
}
}
}
}
int main()
{
printTent(4);
}
Output obtained:
* * * *
Desired output:
*
* *
* *
* *
I don't think you will need that
if (n == 1) {
printf("*");
}
We can take care of that in what you've written in the else part.
For n=4, the number of spaces to be printed at the start of each line is 3, 2, 1 & 0.
You seem to be trying to accomplish that with your first inner loop. But
for(j = 0; j < n; j++) {
printf(" ");
}
will always print n spaces. We need to reduce the number of spaces printed by 1 on each iteration of the outer loop.
Coming to your second loop,
for(j = 1; j <= n; j++) {
printf(" ");
}
This has a similar problem only difference being the incrementation of the number of spaces printed.
Try something like this
void printTentNMMod(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
for(j = i; j < n; j++) {
printf(" ");
}
printf("*");
if(i!=0)
{
for(j=0; j<2*(i-1)+1; ++j)
{
printf(" ");
}
printf("*");
}
printf("\n");
}
}
Also, you could shorten this to
void printTent(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
printf("%*c", n-i, '*');
if(i!=0)
{
printf("%*c", 2*i, '*');
}
printf("\n");
}
}
The * in %*c will set the number of places occupied by the character printed by the %c.
I've finished it and I have written annotation.
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
}
else {
for (i = 0; i < n; i++) {
for (j = 0; j < n -i; j++) {// you should use n-i instead of n because the number of spaces is decreasing
printf(" ");
}
if (j == n-i) { //
printf("*");
for (j = 1; j <= i * 2 - 1; j++)//this loop outputs spaces between two "*"
{
printf(" ");
}
if (i != 0)//the first line only needs one "*"
printf("*");
printf("\n"); //Line breaks
}
}
}
}
Another way.
#include <stdio.h>
int main() {
int i, j;
int height = 5;
for(i = height; i > 0; i--) {
for(j = 1; j < height * 2; j++) {
if(j == i || j == height * 2 - i)
printf("*");
else
printf(" ");
}
puts("");
}
return 0;
}
Output
*
* *
* *
* *
* *
I know this seems like an old question, but none answered questions I searched work.
I have kept receiving "Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted." when I was trying to do a [4][2]*[2][3] matrix multiplication.
Does anyone spot the problem?
#include <stdio.h>
int main() {
int a[4][2] = {0};
int b[2][3] = {0};
int c[3][3] = {0};
int i, j;
printf("Please enter first matrix value\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 2; j++) {
printf("%d row, %d column:", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Please enter second matrix value\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d row, %d column:", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
printf("\n the result is :\n");//
for (i = 0; i < 4; i++) {
printf("[");
for (j = 0; j < 3; j++) {
c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
printf(" %4d ", c[i][j]);
}
printf("]\n");
}
return 0;
}
I haven't checked your code thoroughly, but you define c as 3x3, and here
for (i = 0; i < 4; i++) {
printf("[");
for (j = 0; j < 3; j++) {
c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
...you access c[3], which is c's fourth element, and does not exist. This is bound to write somewhere else.
So check your indexes (as #ptb observed, c's should actually be four rows deep).