Turn number in array to stars - c

I hope someone can help me.
I want to create stars "*" instead of my numbers in the output of array, 10 numbers.
Example
Input
Number 1: 3
Number 2: 2
Number 10: 5
Output
Number 1: ***
Number 2: **
Number 10: *****
But i get always the same number of stars.
Thanks! Dear Harman
#include <stdio.h>
int main()
{
int tal[10], i, j, z;
printf("Mata in tio tal mellan 0 och 60:\n");
for(z=0;z<10;z++)
{
printf("Tal %d: ", z+1);
scanf("%d",&tal[i]);
}
printf("\nRita ut stapeldiagram:\n");
for(z=0;z<10;z++)
{
printf("Tal %d: ", z + 1);
for (j=0; j<i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

Here's your bug. Your inner for-loop that prints a star is redefining the iteration index, i. Simple fix is to use a new variable, say j, in this line:
for (i=0; i<tal; i++)
To this:
for (int j=0; j<tal; j++)

First of all, when scanning user input you are storing the value at the same position of the array tal . Increment the value of i so that in every iteration the new value takes a new memory place .
for(z=0, i=0; z<10; z++, i++)
{
printf("Tal %d: ", z+1);
scanf("%d",&tal[i]);
}
Secondly when printing out the stars in the inner loop, you are iterating to a fixed number i . So , it is printing the fixed number of stars in every iteration of outer loop. Iterate the inner loop to tal[z] . I hope this will make you smile .
for(z=0;z<10;z++)
{
printf("Tal %d: ", z + 1);
for (j=0; j<tal[z]; j++)
{
printf("*");
}
printf("\n");
}

Related

Understanding the arrays in C

I'm new to C and need some help to understand how this piece of code works. I know that it reads the values that the user writes, puts them into an array, and then prints them out.
But I don't understand why I need two "counters" (i and j) to do this. Can someone help me to figure it out?
#include<stdio.h>
int main ()
{
int A[5];
int i=0;
int j=0;
while (i < 5)
i++;
printf("Enter your %d number\n", i);
scanf("%d", &A[i]);
}
while (j < 5)
{
j++;
printf ("\n%d\n", A[j]);
}
}
Technically, what you have aren't two counters, but two loops. If you wanted, to, you could just reuse i for the second loop as well, by doing something like this:
while (i < 5)
i++;
printf("Enter your %d number\n", i);
scanf("%d", &A[i]);
}
i = 0;
while (i < 5)
{
i++;
printf ("\n%d\n", A[i]);
}
As for why you have two loops, the reason is simple. The first loop (using i in your code), reads the 5 integers into the array A. After the first loop concludes, your array A holds the 5 int values, which you could've used however you wanted. In your case, you want to print those values. So what you do is use a loop for looping over the array elements and printing the values to the screen, one by one.
You don't need it, you can simply reset the first and reuse it. However you must increment your index only after having using it otherwise you will overflow the limit of the array :
#include<stdio.h>
int main ()
{
int A[5];
int i=0;
while (i < 5) {
printf("Enter your %d number\n", i);
scanf("%d", &A[i]); // the last must be 4 not 5
i++; //<== increment here
}
i=0;
while (i < 5)
{
printf ("\n%d\n", A[i]); //idem
i++;
}
}

How to split an array into two arrays, one for even and one for odd in C

I am doing an exercise that consists of a program that generates 100 random numbers ranging from 1 to 1000 and saves them in an array.
I have to separate this array into two arrays, one for even numbers and one for odd numbers and print the lists.
I have managed to separate into two arrays, but when they are empty, they invent them. How can i avoid it? Any clue to improve it?
Thank you so much.
That's my code:
#include<stdio.h>
#include<conio.h>
int main(){
int array100[100];
int arraypar[100];
int arrayimpar[100];
int i, n=0, m=0;
for(i=0;i<100;i++){
array100[i] = rand() % (1000 + 1);
printf("%i ", array100[i]);
}
printf("\n\n");
for(i=0;i<100;i++){
if(array100[i]%2==0){
m++;
arraypar[m] = array100[i];
}
else{
n++;
arrayimpar[n] = array100[i];
}
}
printf("\n\n Numeros pares: \n");
for(i=1;i<=100;i++){
printf("%i ", arraypar[i]);
}
printf("\n\n Numeros impares: \n");
for(i=1;i<=100;i++){
printf("%i ", arrayimpar[i]);
}
getch();
return 0;
}
Un-initialized variables can contain anything, it is better to start of by initializing.
This
int array100[100];
int arraypar[100];
int arrayimpar[100];
Should be:
int array100[100] = {0};
int arraypar[100] {0};
int arrayimpar[100] = {0};
And since C uses 0 base array indexing, you are probably seeing a run time error; Dereference of out-of-bounds pointer (or similar) if the code shown in your post is the actual code you are using.
This:
for(i=1;i<=100;i++){
^ ^^
printf("%i ", arraypar[i]);
}
Should be this:
for(i=0;i<100;i++){
^ ^
printf("%i ", arraypar[i]);
}
Same for the next for loop.
Also, a suggestion to help you troubleshoot. I would start off by initializing array100 with a known set of sequential values to easily test whether the values are being split properly:
int array100[100] = {1,2,6,4,5,6,7,8,9,10,
11,12,16,14,15,16,17,18,19,
20,21,22,26,24,25,26,27,28,29,
60,61,62,66,64,65,66,67,68,69,
40,41,42,46,44,45,46,47,48,49,
50,51,52,55,54,55,56,57,58,59,
60,61,62,66,64,65,66,67,68,69,
70,71,72,77,74,75,77,77,78,79,
80,81,82,88,84,85,88,87,88,89,
90,91,92,99,94,95,99,97,98,99,100};
Now run your program with the rand() section commented out to see what you get. If there are problems, they will be easier to see then looking at a collection of randomly generated numbers.
Index of array starts from 0 not 1, so your code below:
for(i=0;i<100;i++){
if(array100[i]%2==0){
m++;
arraypar[m] = array100[i];
}
else{
n++;
arrayimpar[n] = array100[i];
}
}
should change to:
for(i=0;i<100;i++){
if(array100[i]%2==0){
arraypar[m] = array100[i];
m++;
}
else{
arrayimpar[n] = array100[i];
n++;
}
}
So, when you print, you should print m even numbers and n even numbers instead of print all values in arraypar and arrayimpar.:
printf("\n\n Numeros pares: \n");
for(i=0;i < m;i++){ // if m==0, you print nothing
printf("%i ", arraypar[i]);
}
printf("\n\n Numeros impares: \n");
for(i=0;i< n;i++){ // if n == 0, then you print nothing
printf("%i ", arrayimpar[i]);
}
You allocate 100 places for the arrayimpar and arraypar (with indexes 0..99). Usable number of elements (those you want to read) in those arrays are: m(for arraypar) and n (for arrayimpar). Why not use those variables when printing those arrays:
printf("\n\n Numeros pares: \n");
for(i = 1; i <= m; i++){
printf("%i ", arraypar[i]);
}
printf("\n\n Numeros impares: \n");
for(i = 1; i <= n; i++){
printf("%i ", arrayimpar[i]);
}
Practically, this should work, but theoretically you could get one of the arrays completely full, and the other empty. When that happens, eventually you will access 101-st element (with index 100) which doesn't exist. Try to fill these arrays from index 0 instead of 1, so you could never access 101-st element.

Issue with with use of pointers for matrix multiplication

I have been trying to make this code for multidimensional matrices multiplication to work without much success for a couple of weeks by now.
The code works for any given multiplication between matrices of the same dimensions, however when I run something like 2x3*3x4 it only stores about half of the values in the resultant matrix, I have gone through many iterations of the programs including complete rewrites from scratch or working from a previous program that would do the same but without pointer notation and working. It is for an assignment, so I have the limitations of using the pointer notation and not using dynamic memory. I am not sure what I'am missing here even after going through documentation and examples. can you guys give me some suggestions of what is happing here?.
#include<stdio.h>
int main(){
int i, j, k, l, n1=1, n2=1, n3=1, n4=1, m1;
int p1[11][11], p2[11][11], p3[11][11];
printf("\n\nInput first matrix number of rows and columns : \n");
scanf("%d %d", &n1, &n2);
printf("\n\nInput second matrix number of rows and columns : \n");
scanf("%d %d", &n3, &n4);
printf("\n\nInput your values for the first matrix: \n");
for(i=0; i<n1; i++){
for(j=0; j<n2; j++){
printf("\n Input element of Row [%d] and Colunm[%d] = ", i+1, j+1);
scanf("%d", (*(p1+i)+j));
}
}
printf("\n\nInput your values for the second matrix : \n");
for(i=0; i<n3; i++){
for(j=0; j<n4; j++){
printf("\n Input element of Row [%d] and Colunm[%d] = ", i+1, j+1);
scanf("%d", (*(p2+i)+j));
}
}
printf("\n\n\n");
for(i=0;i<n1;i++){
for(j=0;j<n4;j++){
m1=0;
for(k=0;k<n3;k++){
m1+=(*(*(p1 + i) + k)) * (*(*(p2 + k) + j));
}
*(*(p3+j)+i)=m1;
printf("[%d][%d][%d][%d] ", *(*(p1+i)+j), *(*(p2+i)+j), *(*(p3+i)+j), m1);
}
}
printf("\n\n");
printf("\n\nThe result is: \n\n\n\t");
for(i=0; i<n1; i++){
printf("\n\t");
for(j=0; j<n4; j++){
printf("[%d]\t", *(*(p3+i)+j));
}
printf("\t\n\n\n\t");
}
printf("\n");
system("pause");
}
I don't get messages errors but for a given matrix multiplication of different dimensions 2x3*3x4 the result is:
12 12 0 0
12 12 9 2508544
or similar while the expected output is:
12 12 12 12
12 12 12 12
same applies for any matrices that have different dimensions, but square matrices come out alright.
In your calculation loop, you address *(*(p3 + j) + i), using the inner for loop variable first.
In your print loop, you use *(*(p3 + i) + j), using your outer for loop variable first.
So your print loop prints on the transpose of your result matrix.

Print a right-angled triangle with certain condition in c

Enter a certain number, and that number is a condition which determines the number of chars in a single row.Let's say the number is 3
In the first row there is only 1 char.
In second row there is a condition. a+1 Where a is the number that we entered
In third row is 2a+1
Fourth 3a+1
And so on...
Example:
Number that we entered is 3.
a (1)
aaaa (3+1)
aaaaaaa (2*3+1)
Here is what i've came up. I have trouble with implementing that condition.
#include<stdio.h>
main()
{
int i,j,n;
printf("Enter the numbers of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("a");
printf("\n");
}
getch();
}
Just start iterating inner for loop starting from n till i * n as below,
for(i=1;i<=n;i++)
{
for(j=n;j<=i*n;j++)
printf("a");
printf("\n");
}
Here is the demo
Suggestion: get used to start counting at 0
for(i=1;i<=n;i++) // could be for (i = 0; i < n; i++)
{
for(j=1;j<=i;j++) // could be for (j = 0; j < i; j++)
You have to have a multiplication by 3 "somewhere". Try and find the right place and what to multiply by 3.
for(i=0;i<n;i++){
for(j=0;j<n*i+1;j++)
printf("a");
printf("\n");
}

Understanding my mistake with for-loop

I'm new to C and I'm trying to practice my knowledge by doing programs by my own without the use of the internet. I'm stuck with a small and probably dumb mistake but i can't seem to understand what it is. I'm trying to build an hour glass but before i do so i need to understand printing triangles. I'm really going step by step with this and I have tried to understand my mistake for very long with this.
I have my code below and I'm trying to print j where i need j to print normally 12345 then 1234, 123 etc.. but when I use temp-- it skips by printing 123,1,1 and closes the program.
Can somebody have a look at it and tell me what the problem with me second for loop?
#include <stdio.h>
int main()
{
int i,j,k,num, temp=0;
printf("Enter a number: ");
scanf("%d", &num);
temp=num;
for (i=1; i<=num; i++) // Step1: Take care of the rows --> check
{
for (j=1; j<=temp; j++)
{
printf("%d", j);
temp-=1;
}
printf("\n");
}
}
You both increment j and decrement temp which leads to odd effects. You probably don't want to decrement temp. Or you need to reset temp before the inner loop, rather than before the outer loop.
Or, indeed, you do not really need temp at all. Here are two variants of the code, one creating a triangle growing, and one creating a triangle shrinking. Neither needs temp (though t-dn.c could use a variable in place of the expression num - i + 1, but the compiler will probably handle that anyway — it is a basic optimization).
t-up.c
#include <stdio.h>
int main(void)
{
int i,j,num;
printf("Enter a number: ");
scanf("%d", &num);
for (i=1; i<=num; i++)
{
for (j=1; j<=i; j++)
printf("%d", j);
printf("\n");
}
}
t-dn.c
#include <stdio.h>
int main(void)
{
int i,j,num;
printf("Enter a number: ");
scanf("%d", &num);
for (i=1; i<=num; i++)
{
for (j=1; j<=num-i+1; j++)
printf("%d", j);
printf("\n");
}
}
Example output
$ ./t-up
Enter a number: 5
1
12
123
1234
12345
$ ./t-dn
Enter a number: 5
12345
1234
123
12
1
$
t-ok.c
Another variant, keeping the temp variable around:
#include <stdio.h>
int main(void)
{
int i,j,num,temp;
printf("Enter a number: ");
scanf("%d", &num);
temp=num;
for (i=1; i<=num; i++)
{
for (j=1; j<=temp; j++)
printf("%d", j);
printf("\n");
temp--;
}
}
It produces the decreasing pyramid I believe you want. It places the decrement outside the inner loop.
You're decrementing your j-loop condition within the j-loop. This means that every time you print a number, you also decrease the size of your loop (not at all what you want). To fix this, you need to move your temp-=1 from
for (i=1; i<=num; i++) // Step1: Take care of the rows --> check
{
for (j=1; j<=temp; j++)
{
printf("%d", j);
temp-=1;
}
printf("\n");
}
to
for (i=1; i<=num; i++) // Step1: Take care of the rows --> check
{
for (j=1; j<=temp; j++)
{
printf("%d", j);
}
temp-=1;
printf("\n");
}
This decreases the size of your j-loop AFTER you've finished going through it, so you should wind up with the right amount of numbers.
You could even do away with your temporary variable by reversing the i-loop like such
for ( i = num ; i > 0 ; i-- ) {
for ( j = 1 ; j <= i ; j++ ) {
printf("%d", j);
}
printf("\n");
}
for a simpler code.

Resources