garbage value in C array - c

I am trying to write a C code that will print a pyramid structure on screen, something like this.
The corresponding code I've written is something like this.
#include <stdio.h>
#include <stdlib.h>
void printArrayFunc(char arr[9][5]) {
int i, j;
printf("=========================================\nprinting the values\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 9; j++) {
//printf("arr[%d][%d] = %d\n", i,j, arr[i][j]);
if (arr[i][j] == 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
int main() {
int i, j;
char arr[9][5] = {
0
};
printf("============================\nfilling the values\n");
for (i = 0; i < 5; i++) {
for (j = 4 - i; j <= 4 + i; j++) {
arr[i][j] = 1;
// printf("arr[%d][%d]= %d\n",i,j,arr[i][j]);
}
//printf("\n");
}
printArrayFunc(arr);
return 0;
}
It is giving an output like
I know I'm doing some silly mistake but at this moment, I'm not able to find what is going wrong. Let me hear your comments on this.

In the function argument:
char arr[9][5]
In the loop:
for (i = 0; i<5; i++) {
for (j = 0; j<9;j++) {
if (arr[i][j] == 1)
You flipped the position of i and j. i should go from 0 to 9, j from 0 to 5.

if (arr[i][j] == 1)
printf("*");
else
printf(" ");
This statement is giving the garbage value in this statement if if condition is true then it print else statement and when else comes true it prints the garbage value.

Related

I am trying to find the duplicate of elements in an array

Can someone help me to figure out why my code is unable to accurately find the duplicate of elements?
#include <stdio.h>
int main() {
int array[10];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
if (array[i] == array[j]) {
count++;
break;
}
}
}
printf("The duplicates are : %d ", count);
}
I'm a beginner at this language so any advice and suggestions to help me solve this exercise will be much appreciated.
First of all the first loop runs 10 times even if the user enters less numbers. You can fix that by doing:
for (int i = 0; scanf_s("%d", &array[i]) == 1 && i < 10; i++);
Then the logic of the other two loops is wrong. I initially got wrong what you meant. I thought you wanted to know how many times a number is duplicated. So I wrote the wrong program and then modified it for your purposes. Here is your program:
#include <stdio.h>
int main() {
int n[10];
int dupes[5], d = 0;
int flag = 1, omg;
for ( omg = 0; scanf("%d", &n[omg]) == 1 && omg < 10; omg++);
for (int i = 0; i < omg; i++) {
for (int j = i+1; j < 10; j++) {
if( n[i] == n[j] ) {
if( d > 0 ) {
for(int k = 0; k < d; k++) {
if( n[i] == dupes[k] ) {
flag = 0;
break;
}
}
}
if( flag ) {
dupes[d] = n[i];
++d;
break;
}
else {
flag = 1;
break;
}
} // end outer if
}
}
printf("There are %d numbers that have at least one dupe\n", d);
return 0;
}
I named a variable omg out of desperation, writing this program was a nightmare. (Because it came from the ashes of a previous program)
Your code correctly determines the number of duplicate entries in the array.
If instead you want to determine the number of duplicated values, you must modify the algorithm:
#include <stdio.h>
int main() {
int array[10] = { 0 };
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (array[i] == array[j]) {
if (i < j)
count++;
if (i != j)
break;
}
}
}
printf("There are %d duplicate values\n", count);
return 0;
}
I use a structure 'Number' which contains the number and its duplicate, then I fill the array and I put it in ascending order then I calculate the number of duplicate of each number and I fill in the strecture like this :
my code:
#include <stdio.h>
#define size 10
typedef struct Number
{
int number;
int duplicate;
}Number;
int main()
{
int array[size];
Number array2[size];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
int temp=size;
int temppppp=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(array[i]>array[j])
{
temppppp=array[i];
array[i]=array[j];
array[j]=temppppp;
}
}
}
printf("\n\n");
for (int i = 0; i < size; i++)
{
printf("[%d]",array[i]);
}
printf("\n\n");
int i=0;
int j=0;
while(i<size)
{count=1;
while(i<(size-1)&&array[i]==array[i+1])
{
count++;
i++;
}
if(count>=2)
{
array2[j].number=array[i-1];
array2[j].duplicate=count;
j++;
}
i++;
}
int p=0;
while(p<j)
{
printf("\n[%d] has duplicated %d times !\n",array2[p].number,array2[p].duplicate);
p=p+1;
}
printf("\n\n");
printf("\nThere are %d duplicate values\n", j);
}

printing a matrix adress instead of values | C

I wrote a code that tries to multiply two matrices and put them in another result matrix. The code is working (I think) but it prints a very strange output .. I think it has to do with one of the functions pointing to something diffrent than the values, not sure though.
I tried checking each function in separete, also inizialzing each matrix with {0}.
#include <stdio.h>
#define SIZE 5
//#pragma warning(disable:4996)
//#define _CRT_SECURE_NO_WARNINGS
void read_mat(int mat[][SIZE])
{
int i, j, k = 0;
char s[100]; // assign input str to 's'
fgets(s,25,stdin); // recieving only the first 25 numbers
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (s[k] == '\0') { //if the string is just \0 -- the end of the string
mat[i][j] = 0;
}
else { // is there are values in s
if (s[k] != '0') { // binary matrix -- only 0 or 1
mat[i][j] = 1;
k++;
}
else {
mat[i][j] = 0;
++k;
}
}
}
}
}
void mult_mat(int mat_a[][SIZE], int mat_b[][SIZE], int result_mat[][SIZE])
{
int i, j, k;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
result_mat[i][j] += mat_a[i][k] * mat_b[k][j]; // by definition of matrix multiplication
k++;
}
}
}
}
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("%3d", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
int main()
{
int mat_a[SIZE][SIZE] = {0}, mat_b[SIZE][SIZE] = {0}, result_mat[SIZE][SIZE] = {0}; // initializing matricies to {0}
printf("Please Enter Values For Matrix 1\n");
read_mat(mat_a);
printf("Please Enter Values For Matrix 2\n");
read_mat(mat_b);
mult_mat(mat_a, mat_b, result_mat);
printf("The First Matrix Is :- \n");
print_mat(mat_a);
printf("The Second Matrix Is :- \n");
print_mat(mat_b);
printf("The Resultant Matrix Is :- \n");
print_mat(result_mat);
return 0;
}
the outout I am getting:
enter image description here
thanks!
improve your print function
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%3d), ", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}

I want to know how I am reading (or writing) elements in an array wrong in C?

I am trying to draw pascal's triangle in C using a 2D array.
I think what is going wrong is something to do with how I am reading or writing to the array. When I look for a number in the (1st) for loop, what I'm doing seems to work but doesn't outside it?
I am coming at this after some coding in Python and am new to C
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
int n, i , j;
int x[n][n];
n = 4;
for (j=0; j<n; j++)
{
for(i=0; i<n; i++)
{
if (i == 0)
{
x[0][j] = 1;
printf("\n(%d, %d)", i,j);
}
if (i != 0)
{
x[i][j] = 0;
}
printf("%d", x[i][j]);
}
}
printf("\n\n(x[0][0]) %d", x[0][0]);
printf("\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < (n); j++)
{
if (j == 0)
{
printf("\n%d ", x[i][j]);
}
else
{
x[i][j] = x[i-1][j-1] + x[i][j-1];
printf("%d ", x[i ][j ]);
}
}
}
printf("\n");
return;
}
any help would be very appreciated
One key issue I see in your second set of loops is the line that reads:
x[i][j] = x[i-1][j-1] + x[i][j-1];
You avoid this if j == 0, but not if i == 0. For example when j == 1 but i == 0, you will try to access a position in the array at x[-1][0] which is outside of the memory allocated by your array.
I think in particular you want to change your second set of loops to something like this:
for(i = 0; i < n; i++)
{
for(j = 0; j < (n); j++)
{
if ((i == 0) || (j == 0))
{
printf("\n%d ", x[i][j]);
}
else
{
x[i][j] = x[i-1][j-1] + x[i][j-1];
printf("%d ", x[i ][j ]);
}
}
}
Note that the inner if condition has been changed from if (j == 0) to if ((i == 0) || (j == 0)) to prevent indexing outside of your array bounds.
Correction:
without assigning n a value
x[n][n] will not function
or try using dynamic allocation for using such runtime array.
I hope this is helpful. thanks

Regarding a C Program of star pattern

if i give an input of 13, output should be like:
*
**
***
****
***
i've reached only this far:
#include <stdio.h>
int main()
{
int i, j, n = 13;
for(i = 1 ; i <= n ; i++)
{
for ( j = 1; j <= i ; j++)
{
printf("*");
}
t = ((i * (i + 1)) / 2);
printf("\n");
}
printf("%d",t);
return 0;
}
stars should be increasing by 1 in every line, but program will stop when total printed stars reach my inputted number. i am not sure how to start and where to end. i am very new to "for" loops.
You can do something like this
int n,i=1,j,coun=0;
scanf("%d",&n);
while(1)
{
if(i+coun<=n)
{
for(j=0;j<i;j++)
printf("*");
printf("\n");
coun+=i;
}
else
{
for(j=0;j<n-coun;j++)
printf("*");
printf("\n");
break;
}
i++;
}
#include <stdio.h>
int main(){
int i, j, k, n = 13;
for(k = j = i = 1 ; i <= n ; i++, j++){
putchar('*');
if(j==k){
putchar('\n');
j = 0;
++k;
}
}
return 0;
}

I need to pass an array of structures to a function [large code]

I've been working on this for days but can't seem to make it work out.
Sorry in advance for the unholy length of this, so if anyone takes the time to go through it and try to understand this mess, I'd owe you.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct cart {
int id;
char *nume;
} cart;
typedef struct pach {
int id, idCartier, strada, numar, prioritate, codificare;
float greutate;
char* mesaj;
int adresa[18];
} pach;
typedef struct post {
int id, nrPachete;
int vector[50];
} post;
int citeste(int *nrP, cart *cartier, pach *pachet, int *nrC) {
printf("Punctul 1\n");
int i, j;
scanf("%d", nrC);
for (i = 0; i < *nrC; i++) {
cartier[i].id = i;
char aux[500];
scanf("%s", aux);
cartier[i].nume = malloc(strlen(aux) + 1);
cartier[i].nume = aux;
printf("%d %s\n", cartier[i].id, cartier[i].nume);
}
scanf("%d", nrP);
for (i = 0; i < *nrP; i++) {
pachet[i].id = i;
char aux[500];
for (j = 0; j < 18; j++)
scanf("%d", &pachet[i].adresa[j]);
scanf("%d %f", &pachet[i].prioritate, &pachet[i].greutate);
getchar();
fgets(aux, 256, stdin);
pachet[i].mesaj = malloc(strlen(aux) + 1);
pachet[i].mesaj = aux;
printf("%d\n", pachet[i].id);
for (j = 0; j < 18; j++)
printf("%d ", pachet[i].adresa[j]);
printf("\n%d %.6f ", pachet[i].prioritate, pachet[i].greutate);
printf("%s", pachet[i].mesaj);
}
return *nrP;
}
void extrage(int *nrP, pach *pachet) {
printf("\nPunctul 2\n");
int i, j;
for (i = 0; i < *nrP; i++) {
pachet[i].idCartier = 0;
pachet[i].strada = 0;
pachet[i].numar = 0;
for (j = 0; j < 5; j++)
pachet[i].idCartier += pachet[i].adresa[j] * pow(2, (4 - j));
for (j = 5; j < 10; j++)
pachet[i].strada += pachet[i].adresa[j] * pow(2, (9 - j));
for (j = 10; j < 18; j++)
pachet[i].numar += pachet[i].adresa[j] * pow(2, (17 - j));
printf("%d %d ", pachet[i].id, pachet[i].idCartier);
printf("%d %d\n", pachet[i].strada, pachet[i].numar);
}
}
void distribuie(int *nrP, pach *pachet, post *postas, int *nrC, cart *cartier) {
printf("Punctul 3\n");
int i, j;
for (i = 0; i < *nrC; i++) { // FOR-1A
postas[i].nrPachete = 0;
postas[i].id = i;
for (j = 0; j < 50; j++)
postas[i].vector[j] = 0;
}
for (i = 0; i < *nrC; i++) { // FOR-1B
for (j = 0; j < *nrP; j++) {
if (cartier[i].id == pachet[j].idCartier) {
postas[i].vector[postas[i].nrPachete] = pachet[j].id;
postas[i].nrPachete++;
}
}
printf("%d %d ", postas[i].id, postas[i].nrPachete);
for (j = 0; j < postas[i].nrPachete; j++)
printf("%d ", postas[i].vector[j]);
printf("\n");
}
}
void ordoneaza(pach *pachet, int *nrC, post *postas) {
printf("Punctul 4\n");
pach aux;
int i, j, k = 0, schimbat = 1;
for (i = 0; i < *nrC; i++) {
while (schimbat) {
schimbat = 0;
for (j = 0; j < postas[i].nrPachete - k; j++)
if (pachet[postas[i].vector[j]].prioritate < pachet[postas[i].vector[j+1]].prioritate) {
aux = pachet[postas[i].vector[j]];
pachet[postas[i].vector[j]] = pachet[postas[i].vector[j+1]];
pachet[postas[i].vector[j+1]] = aux;
schimbat = 1;
}
k++;
}
k = 0;
schimbat = 1;
for (j = 0; j < postas[i].nrPachete; j++) {
for (k = j; k < postas[i].nrPachete; k++) {
if (pachet[postas[i].vector[j]].prioritate == pachet[postas[i].vector[k]].prioritate)
if (pachet[postas[i].vector[j]].greutate < pachet[postas[i].vector[k]].greutate) {
aux = pachet[postas[i].vector[j]];
pachet[postas[i].vector[j]] = pachet[postas[i].vector[k]];
pachet[postas[i].vector[k]] = aux;
}
}
}
}
for (i = 0; i < *nrC; i++)
for (j = 0; j < postas[i].nrPachete; j++) {
postas[i].vector[j] = pachet[postas[i].vector[j]].id;
}
for (i = 0; i < *nrC; i++) {
printf("%d %d ", postas[i].id, postas[i].nrPachete);
for (j = 0; j < postas[i].nrPachete; j++)
printf("%d ", postas[i].vector[j]);
printf("\n");
}
}
int main() {
int nrP, nrC;
pach pachet[1600];
post postas[32];
cart cartier[32];
citeste(&nrP, &cartier[32], &pachet[1600], &nrC);
extrage(&nrP, &pachet[1600]);
distribuie(&nrP, &pachet[1600], &postas[32], &nrC, &cartier[32]);
ordoneaza(&pachet[1600], &nrC, &postas[32]);
return (0);
}
Short info on what the program does:
The citeste function should read the cartier and pachet structures. All of them. And then print those in a bit different format.
The extrage function should take every pachet, and use the adresa (written in BINARY) to convert its 3 parts and obtain the strada, numar and idCartier. Then also print those.
Distribuie checks if the pachet is distributed to a postas (distributed means pachet.idCartier == postas.id), if not it distributes it.
Ordoneaza takes every postas's vector and sorts it after the prioritate (or greutate if the prioritate-s are equal).
But it doesn't work as intended and also gives weird Segmentation Faults.
For example if I comment out the distribuie function, it gives me segfault right after extrage. If I put it back, it gives segfault right after doing it. And if I uncomment everything, it gives segfault at the end again.
If anyone actually read all of this and would be willing to reply, I'd highly appreciate it. Any bit of advice helps!
I did not read your code, but your title said you had trouble passing array of structures. I am attaching a working snippet hope it will help you get around your problem.
#include<stdio.h>
typedef struct employee{
int empId;
char name[10];
}EMP;
void arrayOfStruct(EMP *a, int size)
{
printf("%d\t%d",a[0].empId,a[3].empId);
}
int main()
{
EMP NC[4];
NC[0].empId = 9;
NC[3].empId = 2;
arrayOfStruct(&NC[0],sizeof(NC)/sizeof(NC[0]));
}
with the help of size you can never go beyond the memory allocated for structures.
In case you, want to pass higher dimensional arrays, you have to hard code all the size of arrays except the outer most.
void arrayOfStruct(EMP a[][4], int size)
{
// to do
}
int main()
{
EMP NC[2][4];
...
arrayOfStruct(NC,sizeof(NC)/sizeof(NC[0]));
}
as you see, I did not specify the higher most size of array, which I am passing via other arguement.
Why do I need to specify size of inner dimensions ?
Lets take an example, for suppose you have int[4][4], and you are trying to pass array to a function via int[3][], how does a compiler know how many inner blocks to create, in other case via int[][3], the compiler can easily understand that it has to make inner block of size 3 for each outer array.

Resources