Wrong C output in terminal - c

So I was experimenting with arrays in C and came across this weird event where the value of an array changes even though I am not manipulating the data of the elements in that array.
Here is my code:
#include <stdio.h>
#include <conio.h>
int main () {
int arr[] = {0};
scanf("%i", &arr[0]);
scanf("%i", &arr[1]);
for (int i = 0; i < 2; i++) {
printf("value of i %i\n", i);
printf("%i\n", arr[i]);
}
getch();
}
and the output was:
2 //this is an input
3 //this is an input
value of i 0
2
value of i 1
1
I have the same code in Turbo C but in Turbo C the value of the elements in that array is correct, the output in Turbo is is:
2 //this is an input
3 //this is an input
value of i 0
2
value of i 1
3
I tried to debug and see when does the change of value happens in the code, so I tried:
#include <stdio.h>
#include <conio.h>
int main () {
int arr[] = {0};
scanf("%i", &arr[0]);
scanf("%i", &arr[1]);
for (int i = 0; i < 2; i++) {
printf("value of i %i\n", i);
printf("%i\n", arr[i]);
}
printf("%i\n", arr[0]);
printf("%i\n", arr[1]);
getch();
}
and now the output is very weird:
2 //this is an input
3 //this is an input
value of i 0
2
value of i 1
1
2
2

arr[1] is not initialised. Try,
int arr[] = {0, 0};

Related

Converting a C program to use pointers in array and have the same output

Here is the code, I am having trouble converting it to use pointers and have the same output:
#include <stdio.h>
int main() {
int i, s[4], t[4], u = 0;
for (i=0; i<=4; i++)
{
s[i] = i;
t[i] = i;
}
printf("s : t\n");
for (i = 0; i <= 4; i++)
printf("%d : %d\n", s[i], t[i]);
printf("u = %d\n", u);
}```
The output of the code is this:
s : t
0 : 4
1 : 1
2 : 2
3 : 3
4 : 4
u = 0
My code is as follows, but the output isn't the same, can someone please help:
int main() {
int i,
*s[4],
*t[4],
*u=0;
for (i=0; i<=4; i++)
{
s[i] = &i;
t[i] = &i;
}
printf("s:t\n");
for(i=0;i<=4;i++)
printf("%d:%d\n",*s[i],*t[i]);
printf ("u=%d\n", *u);
}
Here is one possible solution that I am not sure if this might be what you are looking for:
#include <stdio.h>
int main()
{
int i, s[4], t[4], u=0;
for (i=0; i<4; i++)
{
*(s+i) = i;
*(t+i) = i;
}
printf("s:t\n");
for(i=0; i<4; i++)
printf("%d:%d\n",*(s+i),*(t+i));
int *ptrToU = &u;
printf ("u=%d\n", *ptrToU);
}
Arrays in C are indexed starting at 0. Your loops had i<=4, but had to be changed to i<4 to prevent an out-of-bounds error.
In your original code, you had:
int *s[4];
This creates an array of 4 pointers to int. The question becomes, what do these pointers point to?
Your code:
s[i] = &i;
set each int pointer to point to the memory location of your variable i. Since i was not changing its memory location, every pointer in s[i] pointed to the same location. Hope this helps some.

change an integer to base 2 with arrays as output in C

#include <stdio.h>
#include<math.h>
int main(void){
int Num;
int i;
int N;
int x = 0;
int a[x];
scanf("%d", &Num);
N = Num;
for(i = 0; i < Num; i++)
{if (Num%2 == 0)
{a[i] = 0;}
else
{a[i] = 1;}
Num = Num/2;
}printf("%d in base 2 is %d", N, a[x]);
return 0;
}
program should convert an integer Num to base 2 eg 17 to 10001.
ideally using an array as the output
the remainder of the division of Num by 2 should be the last number in the output
then number is divided by 2 and the process repeats with the second output becoming the 2nd last output of the array
Sorry if this question is worded badly
Any help is appreciated
Thanks
#include <stdio.h>
#include <math.h>
int main(void){
int Num;
int N;
scanf("%d", &Num);
int x = floor(log2(Num)) +1;
int a[x];
N = Num;
for(size_t i=0;i<x;i++){
if(Num%2 == 0){
a[i]=0;
}else{
a[i]=1;
}
Num /= 2;
}
printf("%d in base 2 is ",N);
for(int i=x-1; i>=0;i--){
printf("%d",a[i]);
}
return 0;
}
I think the above code works fine for what you need (although it's not the best way to code this program).
In line 7 of your code, you defined x as 0; so, in line 8, the length of your array is 0 – and that's not what we need.
At the end, when you want to output the result, you only output the xth element of your array. Instead, we want to output every element that we stored. (I used i-- because, if I did not, the binary of Num would be reversed.)

How to add other elements to an array[size] (index 0 is already set)

im really new to programming, c is my first language
i want to create an array with a size set by scanf
it should start with startValue (second scanf) and then printf the rest of the array but with startvalue+1
so for example if i type in:
3 // --> size of the array
4 // --> my starting point/startValue
the programm should give me:
4
5
6
after several hours of watching videos/reading online and trial and error i just dont know how to solve this problem :( i really appreciate your help thank you
#include <stdio.h>
int main() {
int size;
int startValue;
scanf("%d\n %d", &size, &startValue);
int array[size];
array[0] = startValue;
for(int i = 0; i<size; i++) {
printf("%d\n", array[i]);
}
return 0;
}
As you want desired output: 4 5 6
for this you need to assign value to the rest of elements also as did with
array[0] = startValue;
I've tried to assign it the way you wanted, may be this would help you to understand
#include <stdio.h>
int main() {
int size;
int startValue;
scanf("%d\n %d", &size, &startValue);
int array[size];
for(int i = 0; i<size; i++)
{
array[i]= startValue;
printf("%d ", array[i]);
startValue=startValue+1;
}
return 0;
}
I'm assigning values to
array[0] & array[1] through this
array[i]= startValue;
and incrementing the value of startValue by +1 through this
startValue=startValue+1;
line

No output in c program?

I made a 2 dimension array where I have to type 16 numbers and the program should return me the biggest value and its location (This is just an exercise, I know I can just use an array with 16 values instead of 4x4)
#include <stdlib.h>
#define CONSTC 4
#define CONSTL 4
int main ()
{
int i=1;
int e;
int k;
int c;
int a;
int A [CONSTR][CONSTC];
for (k=0; k<CONSTL; k++)
{
for (c=0; c<CONSTC; c++, i++)
{
printf ("%2d - Type number\n", i);
scanf ("%d", &A [k][c]);
}
}
e = Matriz [0][0];
for (k=0; k<CONSTL; k++)
{
for (c=0; c<CONSTC; c++, i++)
{
if (e>A[k][c])
{
e = A [k][c];
return k;
return c;
}
}
}
printf ("\t\tBiggest Value = %d\nValue Location = %d %d", e, k, c);
}
I corrected some stuff in your code
#include <stdlib.h>
#include <stdio.h> //you need to include this to use printf and scanf
#define CONSTC 4
#define CONSTL 4
int main()
{
int i=1;
int e;
int k;
int c;
int a;
int A [CONSTL][CONSTC];
for (k=0; k<CONSTL; k++)
{
for (c=0; c<CONSTC; c++, i++)
{
printf ("%2d - Type number\n", i);
scanf ("%d", &A [k][c]);
}
}
e = A[0][0];//Not sure what Matriz was, you meant A right?
for (k=0; k<CONSTL; k++)
{
for (c=0; c<CONSTC; c++, i++)
{
if (e>A[k][c])
{
e = A [k][c];
//return k;
//return c;
}
}
}
printf ("\t\tBiggest Value = %d\nValue Location = %d %d", e, k, c);
return 0;
}
The main problem was that you let return k and return c in the middle of the loop, return stops the whole process and the whole program.
So you never get to the last printf.
Also, if you need the biggest value, you would need your if statement to be if(e < A[k][c]).
There are several problems in the code:
The return statement would return the given code to the main(), which makes no sense.
Some variables aren't declared which is a huge error.
Defined stdlib.h but I/O operations does exists in stdio.h library.
After the first return statement, the second will never get executed.
Code redefined:
#include <stdio.h>
#define MAX_ROWS 4
#define MAX_COLS 4
int main(void) {
int mat[MAX_ROWS][MAX_COLS];
int max = 0;
int posM, posN;
printf("Enter [4 x 4] Matrix below: \n");
for (int i = 0; i < MAX_ROWS; i++)
for (int j = 0; j < MAX_COLS; j++)
scanf("%d", &mat[i][j]); // getting the values of i * j positon
for (int i = 0; i < MAX_ROWS; i++)
for (int j = 0; j < MAX_COLS; j++)
if (mat[i][j] > max) {
max = mat[i][j]; // if max is lesser than the current one, then assign
posM = i + 1; // we had started the counting from 0, so increased 1
posN = j + 1;
}
printf("The largest element in the matrix: %d\n", max);
printf("The position is: %d x %d\n", posM, posN); // simply get the value
return 0;
}
The program simply asks the user to fill 4 ✕ 4 multidimensional array values. And then a loop is executed once as soon as the first loop quits.
The second loop tries to find the maximum value held by the matrix array. If the current value is lesser than the previously scanned value, it's then assigned. This process continues until the loop ends and finally displays the results.
A sample output:
Enter [4 x 4] Matrix below: // --- INPUT
1 2 3 4
4 3 2 1
3 3 3 4
5 4 3 1
The largest element in the matrix: 5 // --- OUTPUT
The position is: 4 x 1
The return; statement ends the function. When there's an expression afterwards, e.g. return k;, the result of the function is k.
Since the return statement exits from the function main, no more code runs after that point. This is why no output is printed.

Printing numbers in zigzag order in 2 D array

#include <stdio.h>
int main(){
int i,j;
int flag = 0;
int x [3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for (i = 0;i<3;i++){
if (flag ==1){
for (j=2;j<0;j--){
printf(" %d",x[i][j]);
}
flag =0;
}
else {
for (j=0;j<3;j++)
{
printf(" %d ",x[i][j]);
}
flag =1;
}
}
return 0;
}
i'm trying to print the numbers in the array in a zigzag form the expected output should be 123654789 but all i got 123789 for some reason i dont enter the loop in the flag condition i want to know the reason.. thanks in advance
Instead of
for (j=2;j<0;j--){
use
for (j=2;j>-1;j--){
and (only for the nice formatting) instead of
printf(" %d ",x[i][j]);
(near the end, with a space after %d) use
printf(" %d",x[i][j]);
(without that space).
Try this, it will give you the output what you want.
#include <stdio.h>
int main(){
int i,j;
int flag = 0;
int x [3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for (i = 0;i<3;i++){
if (flag ==1){
for (j=2;!(j<0);j--){
printf(" %d",x[i][j]);
}
flag =0;
} else {
for (j=0;j<3;j++) {
printf(" %d",x[i][j]);
}
flag =1;
}
}
return 0;
}
Output:
./a.out
1 2 3 6 5 4 7 8 9

Resources