Question

Code in Matlab. I need to make a diamond shape with * I get part of...

Code in Matlab.

I need to make a diamond shape with *
I get part of it with the code below.

Instructions are: " Given the value of N print diamond of N + (N-1) rows. For example, if N = 5 it should print a diamond."

clc
clear
n = input ('number of rows \n')
o = input ('number of inverted rows \n')
t = (o-1)

for i = 1:n
for k = 1:n-i
fprintf (' ');
end
for j = 1:i
fprintf('* ');
end
fprintf('\n');
end
for q = k-1:o
for m = 1:o-q
fprintf(' ');
end
for j = 1:q
fprintf (' *');
end
fprintf('\n');
end
for r = 3:t
for r = 1: t-r
fprintf (' ')
end
for s = 1:r
fprintf ('*')
end
fprintf('\n');
end

Homework Answers

Answer #1

Updated MATLAB Program:

% MATLAB Program that displays Diamon

clc;
clear;

% Reading number of rows
n = input('Number of rows: ');

% Iterating over rows
for i = 1:n
% For printing spaces
for k = 1:n-i
fprintf (' ');
end
% For printing Asterick
for j = 1:i
fprintf('* ');
end
fprintf('\n');
end

% Decrement n by 1 for lower triangle
n = n - 1;

% Code for printing below triangle
for i = n:-1:1
% For perinting spaces
for k = 1:n-i+1
fprintf (' ');
end
% For printing Asterick
for j = 1:i
fprintf('* ');
end
fprintf('\n');
end

__________________________________________________________________________________________

Sample Run:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is LU deposition code of matlab. What's wrong with this code.... Thank you n and...
This is LU deposition code of matlab. What's wrong with this code.... Thank you n and B was given from above, what is for i = 1:n U(1,i) = A(1,i); L(i,1) = A(i,1)/U(1,1); %u(1,1)=1 end for j = 2:n for k = j:n U(j,k) = A(j,k)-L(j,1:j-1)*U(1:j-1,k); L(k,j) = (A(k,j)-L(k,1:j-1)*U(1:j-1,j))/U(j,j); end end for i = 1:n x(i) = B(i) / L(i,i:n)*U(i:n,i); end fprintf("Ans x = \n"); fprintf("%f \n", x);
i need this code to print a number of stars depending on how much the user...
i need this code to print a number of stars depending on how much the user wants and after * it prints no stars. if the user enters a negative number it should print "error invalid number" this is my code so far: def stars(n,i): stars(n, 1) if n <= 0: return "No stars" if i <= n: print("* ", end="") stars(n, i + 1) else: print("no stars") stars(n - 1, 1) n = int(input("enter an integer")) def main(): stars()...
USING MATLAB Code for pseudo Random Input N M=0 For i=1 to N X=rand# Y=rand# If...
USING MATLAB Code for pseudo Random Input N M=0 For i=1 to N X=rand# Y=rand# If X^2+ y^2 <= 1, then M=M+1 End the loop Print M/N (This is the probability) Print M/N (this is approximately = Py
I need to change the MATLAB code below to something else, but I need the solutions...
I need to change the MATLAB code below to something else, but I need the solutions to match each other. (Also try not to copy answer posted on chegg already) % % Problem 1.8 % clc; clear all; close all; % reset matlab w orthregdata; % load data n = length(a); w = ones(n,1); ma = w'*a/n; mb = w'*b/n; % ma = mean(a); % mb = mean(b); sa = norm(a-ma)/sqrt(n); sb = norm(b-mb)/sqrt(n); % sa = std(a,1); % Pass...
MATLAB save output as array this is my code so far, I'm going to need to...
MATLAB save output as array this is my code so far, I'm going to need to save these as arrays or vectors i think to take some statistics on them. how do i save each walkers position relitive to time I should end up with 1000x 1000 matrix i think or 1000 arrays walkers=1000; %walkers=input('how many walkers do you want to simulate?') N = 1000; %N = input('how many steps do you want?') for k= 1:walkers displacement= randn(1,N); x =...
Problem 3 you can use Matlab and also i give u the Problem 1 code its...
Problem 3 you can use Matlab and also i give u the Problem 1 code its on Matlab Using the same initial code fragment as in Problem 1, add code that calculates and plays y (n)=h(n)?x (n) where h(n) is the impulse response of an IIR bandpass filter with band edge frequencies 750 Hz and 850 Hz and based on a 4th order Butterworth prototype. Name your program p3.sce this is the Problem 1 code and the solutin clear; clc;...
Please provide commenting of code so I can understand how to solve this problem. Please provide...
Please provide commenting of code so I can understand how to solve this problem. Please provide text files. Using C++ Write a program that reads a given file, and then outputs the contents of it to another file. It should also print out the number of lines and the number of times each alphabetic character appears (regardless of case) in the input file at the end of the output file. It should prompt the user for the input and output...
Answer the following questions: 1. Given the code segments below with n as the problem size,...
Answer the following questions: 1. Given the code segments below with n as the problem size, answer the following questions: //Code Segment 1 (Consider n as a power of 3) int sum = 0; for(int i = 1; i <= n; i = i*3) sum++;​​​​​​​// statement1 //Code Segment 2: (Consider both possibilities for x) if(x < 5){ for(int i = 1; i <= n; i++)    for(int k = 1; k <= i; k++)    sum++;​​​​​// statement2 } else{ for(int...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
C Programming I am trying to also print the frequency and the occurrence of an input...
C Programming I am trying to also print the frequency and the occurrence of an input text file. I got the occurrence to but cant get the frequency. Formula for frequency is "Occurrence / total input count", this is the percentage of occurrence. Can someone please help me to get the frequency to work. Code: int freq[26] = {0}; fgets(input1, 10000, (FILE*)MyFile); for(i=0; i< strlen(input); i++) { freq[input[i]-'a']++; count++; } printf("Text count = %d", count); printf("\n"); printf("Frequency of plain text\n");...