Array prints an unwanted number for no reason - arrays

/*This is a c program that inputs an array of 9 numbers, reverses it and prints it*/
#include <stdio.h>
int main()
{
int a[9];
printf("Enter 9 numbers \n");
int i;
for(i=0;i<9;i++)
{
scanf("%d",&a[i]);
}
int n=10;
int t;
for(i=0;i<9/2;i++)
{
t=a[i];
a[i]=a[8-i];
a[8-i]=t;
}
for(i=0;i<10;i++)
{
printf("%d\t",a[i]);
}
}
This is the output:
Enter 9 numbers 1 2 3 4 5 6 7 8
9
9 8 7 6 5 4 3 2 1
32765
I want to understand where the 32765 is coming from and how to fix it.

The last weird number you see there is the value of the next (10st) address from the cell after your array. "a" has been declared with 9 values, and you should remember that arrays in C start from 0. While printing you are trying to access 10 numbers (from 0 to 9)

As other answers point out, you are using a wrong array length at the last loop.
To avoid these kind of mistakes, use macros or const variables to store the fixed length of the array. For example:
#define ARRAYLEN 9
// ...
int a[ARRAYLEN];
// ...
for (int i = 0; i < ARRAYLEN; i++) {
// ...
}

Related

printing integer and char using the same argument in c

I have an assignment to implement a board game. I come up with an idea to print integer and char using the same statement for example,
int main() {
char c[2];
c[0]=1;
c[1]='X';
for (int i = 0;i<=1;++i) {
printf("%d", c[i]);
}
return 0;
}
this is not working as I have integer and char in the array. how could i modify this to just use one for loop?
EDIT:
I am trying to implement a noughts and crosses game. So I display my board as
1 2 3
4 5 6
7 8 9
and whenever a player put down a X or O at a position, I want to display my board for example,
1 X 3
4 5 6
7 8 9
You can just print chars. Since your digits are always 1 to 9 you can use chars '1' to '9' (49 to 57) instead.
It's not exactly a code you'd be writing in production, but it's a nice case of a little puzzle and what you can do with C.
int main() {
char c[2];
c[0]='1';
c[1]='X';
for (int i = 0;i<=1;++i) {
printf("%c", c[i]);
}
return 0;
}
I've slightly modified your code to make it clear how that might work.
Your code won't work as intended as you're mixing up two distinct data types. int and char are not the same type of element. They don't even have the same size; the size of char is defined as 1, and the size of int varies by implementation, but is usually 4 or 8. So your array will not even be able to hold ints. When you say:
c[0] = 1;
what is actually happening is that the array stores the char with the ASCII value of 1, which is the "Start of Heading" character.
To do what you want, do not use the int type at all. Use char throughout. That is, write something like this:
c[0] = '1';
Putting the '1' in single quotes makes it a char, not an int. You can use this to fill your 2D array and then make replacements with 'X' and '0' when needed.
You're doing to mistakes here.
You're using c[0]=1; instead of c[0]='1';. You want the character 1 and not the number 1.
You're using printf("%d instead of printf("%c. You want to print a character and not a number.
Here is some example code to achieve what you want:
void print_board(char *board) {
for(int i=0; i<9; i++) {
printf("%c ", board[i]);
if(i % 3 == 2)
printf("\n");
}
printf("\n");
}
void init_board(char *board) {
for(int i=0; i<9; i++)
board[i] = '1' + i;
}
int main(void) {
char board[9];
init_board(board);
print_board(board);
board[2] = 'X';
print_board(board);
board[5] = 'O';
print_board(board);
init_board(board);
print_board(board);
}
The above is not really production code. But it's enough to get you going.
Output:
$ ./a.out
1 2 3
4 5 6
7 8 9
1 2 X
4 5 6
7 8 9
1 2 X
4 5 O
7 8 9
1 2 3
4 5 6
7 8 9

How to shift an array elements by n places in C

If we have an array, for example, Arr[] = {1,2,3,4,5} and I want to shift the elements by 2, how can I do that?
the the array should be: {3,4,5,1,2}.
I tried to slove this way:
#include <stdio.h>
int main(void) {
int broj,pom,i,niza1[10],niza2[10],raz,tem=0,rest=0;
scanf("%d%d",&broj,&pom);//broj= number of elements and pom=shifting
for (int i=0;i<broj;i++){
scanf ("%d",&niza1[i]);
}
raz=broj-pom;//difrence between thenumber of elements and shifting
for (int i=raz;i<=broj;i++){
niza2[tem]=niza1[i-1];
tem++;
}
for (int i=0;i<broj;i++){
printf("%d",niza2[i]);
}
return 0;
}
input: 5 2
1 2 3 4 5
resault: 3 4 5 0 0
How can I add the last two numbers inside the array?
You are only copying broz - raz elements into new array.
raz=broj-pom;//difrence between thenumber of elements and shifting
for (int i=raz;i<=broj;i++){
niza2[tem]=niza1[i-1];
tem++;
}
should be
I removed unnecessary tem variable.
(i+raz)%broj you need % to wrap the copying.
raz=broj-pom;//difrence between thenumber of elements and shifting
for (int i=0;i<broj;i++){
niza2[i]=niza1[(i+raz)%broj];
}

C - Mixed Text File Data > To Different Arrays (Int and String)

I have just an ordinary "project.txt" file with this data:
Programming 10 3 4 5 4 3 2 4 5 2 3
Mathematics 8 3 3 4 5 3 2 2 3
Physics 6 3 4 5 3 4 5
Design 6 5 4 5 3 2 4
Logistics 8 3 4 5 3 1 1 2 3
Need to open this file, read and write all this data to arrays.
I need to somehow divide String and Integers from each other.
* NO NEED FOR IT RIGHT NOW, but later I will need to write text and numbers to different files. ***NO NEED TO DO THIS NOW*
Just need to do it with 2 different int and char arrays, but I am sitting for few hours and can't find normal explanation of how to divide this string from other things.
Here is my code, can anybody help me?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
fp = fopen("C:\\Project\\project.txt", "r");
char arrayWords[140];
int i;
if(fp == NULL){
printf("Can't Read The File!");
exit(0);
}
for (i = 0; i < 140; i++){
fscanf(fp, "%s,", &arrayWords[i]);
}
for (i = 0; i < 140; i++){
printf("Number is: %s\n\n", &arrayWords[i]);
}
fclose(fp);
return 0;
}
This is the output I get, it is really confusing (Some part of it)...
Number is: P13454324523M833453223P6345345D6545324L834531123
Number is: 13454324523M833453223P6345345D6545324L834531123
Number is: 3454324523M833453223P6345345D6545324L834531123
Number is: 454324523M833453223P6345345D6545324L834531123
Number is: 54324523M833453223P6345345D6545324L834531123
Number is: 4324523M833453223P6345345D6545324L834531123
Number is: 324523M833453223P6345345D6545324L834531123
Number is: 24523M833453223P6345345D6545324L834531123
Number is: 4523M833453223P6345345D6545324L834531123
Number is: 523M833453223P6345345D6545324L834531123
Number is: 23M833453223P6345345D6545324L834531123
Number is: 3M833453223P6345345D6545324L834531123
Number is: M833453223P6345345D6545324L834531123
Number is: 833453223P6345345D6545324L834531123
Number is: 33453223P6345345D6545324L834531123
Number is: 3453223P6345345D6545324L834531123
Number is: 453223P6345345D6545324L834531123
Number is: 53223P6345345D6545324L834531123
Number is: 3223P6345345D6545324L834531123
Number is: 223P6345345D6545324L834531123
Number is: 23P6345345D6545324L834531123
Number is: 3P6345345D6545324L834531123
Number is: P6345345D6545324L834531123
Number is: 6345345D6545324L834531123
Number is: 345345D6545324L834531123
Number is: 45345D6545324L834531123
Number is: 5345D6545324L834531123
Number is: 345D6545324L834531123
Number is: 45D6545324L834531123
Number is: 5D6545324L834531123
Think problem is in pointers, but don't know, I am kind of a beginner and can't find any problems.
Thanks to everyone
It's easy to parse these lists of space-separated numbers using the strtol() function. This is a good solution because it handily sets a pointer to the next block of whitespace/non-number after the number it just converted. If there's no number to convert, it just sends back the original pointer. So the code tests for this to know when it's complete.
EDIT: Updated to handle name too.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *number_str = "SomeWords 19 9 6 2 22 1 0 -4";
char *ptr;
char *next_number;
int numbers[1000];
int number_count = 0;
char name[100];
next_number = number_str;
// Copy off the name
ptr = strchr(number_str, ' ');
if (ptr != NULL)
{
strncpy(name, number_str, ptr-number_str); // TODO - check bounds of name[]
name[ptr-number_str] = '\0';
// Point to the first number
next_number = ptr+1;
printf("Stored name [%s]\n", name);
}
// Then parse all the numbers
do
{
ptr = next_number;
long num = strtol(ptr, &next_number, 10);
if (ptr != next_number) // found one
{
numbers[number_count] = (int)num;
printf("Stored %3d into numbers[%d]\n", numbers[number_count], number_count);
number_count += 1;
}
} while(ptr != next_number);
return 0;
}
Outputs:
Stored name [SomeWords]
Stored 19 into numbers[0]
Stored 9 into numbers[1]
Stored 6 into numbers[2]
Stored 2 into numbers[3]
Stored 22 into numbers[4]
Stored 1 into numbers[5]
Stored 0 into numbers[6]
Stored -4 into numbers[7]

How to read 2D arrays in C?

I'm learning about 2D arrays in C and I'm a bit confused. I have the following program which reads a 2D arrays and adds its values in another array.
#include <stdio.h>
int main() {
int arr[4][5] = {{1,2,3,4,5},
{3,1,1,5,2},
{4,1,4,1,5},
{2,5,3,3,4}};
int many[4];
int i;
for (i=0;i<4;i++) {
many[i] = arr[i][i] + arr[i][i];
printf("%d\n", many[i]);
}
The output of this program is:
2
2
8
6
But I think it should be 3, 3, 9, 7 because the for loop starts at 1 and the first column and row gets 1 and second column and row get 2 because there is already 1 which means 1+1 = 2 and 2 + 1 = 3, for second number it is same idea.
For the third number I got 9 because we get 4 from row 2 column 2. 4 + 4 + 1 = 9 and for last number I got 7 because last row has 3 in row 3 column 3.
The output you get is absolutely right for this loop.
for (i=0;i<4;i++)
{
many[i] = arr[i][i] + arr[i][i];
printf("%d\n", many[i]);
}
You can easily get to know it by tracing.
so let's trace it...
during i=0
arr[0][0] denotes 1st element (as indices start from 0) of 1st array which is 1
many[0] = arr[0][0]+arr[0][0] // 1+1=2
during i=1
arr[1][1] denotes 2nd element of 2nd array which is also 1
many[0] = arr[1][1]+arr[1][1] // 1+1=2
during i=2
arr[2][2] denotes 3rd element of 3rd array which is 4
many[2] = arr[2][2]+arr[2][2] // 4+4=8
during i=3
arr[3][3] denotes 4th element of 4th array which is 3
many[3] = arr[3][3]+arr[3][3] // 3+3=6
Therefore,The output of this program is:
2
2
8
6
Note: arr[m][n] denotes (n+1)th element of (m+1)th array
You need 2 loops to iterate through your 2D array
#include <stdio.h>
int main() {
int arr[4][5] = {{1,2,3,4,5},
{3,1,1,5,2},
{4,1,4,1,5},
{2,5,3,3,4}};
int many[4];
int i;
int j;
for(i=0;i<4;i++)
{
many[i] = 0;
for(j=0;j<5;j++)
{
many[i] += arr[i][j];
}
printf("%d\n", many[i]);
}
}

Prints out the first eight autmorphic numbers

Create a program that prints out the first eight[8] automorphic numbers. Output must be done
in the main() function.
Here's the code I worked on:
#include <stdio.h>
#include <conio.h>
main() {
int automorphic[8];
int n;
printf("\t\t\tAUTOMORPHIC\n\n\n");
for(n=1; n<8; n++ ){
if (n*n%10==n || n*n%100==n || n*n%1000==n);
}
printf("\t%d\n\n", automorphic [n]);
getch();
return 0;
}
I don't get why it only prints out 0? Is there something missing on my code? Or am I not doing the right thing at all?
Because:
You are examining only numbers 1 to 7 (only numbers 1, 5 and 6 in this range are automorphic).
You are not storing the number in the array.
You are printing only element 7 of the array (which is always 0).
You need to expect numbers bigger than that.
Additionally, automorphic numbers start at 0, not 1.
int main() {
long automorphic [8];
long i;
int n;
printf("\t\t\tAUTOMORPHIC\n\n\n");
i= 0 ;
n= 0 ;
while( n < 8 ) {
if( i*i%10==i || i*i%100==i || i*i%1000==i || i*i%10000==i || i*i%100000==i ) {
automorphic[n]= i ;
n++;
}
i++;
}
for(n= 0 ; n < 8 ; n++ ) {
printf("\t%d\n\n", automorphic[n] );
}
getch();
return 0;
}
Result should be:
0
1
5
6
25
76
376
625
You're close, but not quite there.
You are only testing the integers from 1 through 8 for the automorphic property. The question asks for the first 8 automorphic numbers, not which numbers less than 8 are automorphic.
Your if statement doesn't do anything if the condition is true.
You are printing the value of automorphic[n] but do not set any values in the automorphic array. Also, at the point the printf executes, n will be 8 because the for loop has already finished.

Resources