The int array displays an odd value at the end [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am running this code for teaching purposes about sizeof and displaying array values; everything looks OK with the char array (the last element of the array '0' or NULL is not displayed, the float array displays it as 0.0000... but the char array gives me the last element of 54417138 that looks like an address (hex equivalent 206F6554). I tried to retrieve the content of it assuming it is an address (see the comment part of the code) but got a stack overflow error so I am lost at this moment - any help will be appreciated.
#include <stdlib.h>
int main(void) {
int i =0;
int j = 0;
int k = 0;
char name[] = {'T','e','o',' ','G','u','g' };
int z = sizeof(name)/sizeof(name[0]);
printf("%d\n", z);
for ( k=0; k <= z; k++) {
printf("%c", name[k]);
}
for ( k=0; k <= z; k++) {
printf("%c", *(name+k));
}
printf("\n");
int number[] = {1,2,3,4,5,6,7,8 };
int x = sizeof(number)/sizeof(number[0]);
printf("%d\n", x);
for ( i=0; i <= x; i++) {
printf("%d\t", number[i]);
}
for ( i=0; i <= x; i++) {
printf("%d\t", *(number+i));
}
printf("\n");
/*int *ptr = 0x206F65540;
printf("Memory address is: %i\n", ptr);
printf("Content of that address is: %d\n", *ptr);
printf("\n");*/
float number1[] = {1.0, 2.5, 3.1234, 10.10};
int y = sizeof(number1)/sizeof(number1[0]);
printf("%d\n", y);
for (j=0; j <= y; j++) {
printf("%f\t", number1[j]);
}
for (j=0; j <= y; j++) {
printf("%f\t", *(number1+ j));
}
printf("\n");
return EXIT_SUCCESS;
}
This is the output of the program:
7
Teo Gug Teodor Gug
8
1 2 3 4 5 6 7 8 544171348 1 2 3 4 5 6 7 8 544171348
4
1.000000 2.500000 3.123400 10.100000 0.000000 1.000000 2.500000 3.123400 10.100000 0.000000
note the strange value in the display of int array of 544171348*

You are accessing beyond the end of your arrays with this kind of loop
for ( i=0; i <= x; i++) {
should be
for ( i=0; i < x; i++) {
^
^
as array indices are zero based.
Reading beyond the end of your arrays is undefined behaviour.

Related

C programming array swap [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have been trying to do this for 4 hours with no luck and I really need to have it done. It is my school's task.
#include <stdio.h>
#include <stdlib.h>
int main() {
char strings[5][5][5][5][5];
char *temp[5];
int b[5];
int a[5];
int x;
int i;
int z;
int a_value;
int b_value;
//get 5 strings from the input
for (i = 0; i < 5; i++) {
scanf("%s", strings[i]);
}
//Get 5 numbers from the input
for (x = 0; x < 5; x++) {
scanf("%d %d", & b[x], & a[x]);
//printf("B is %d and A is %d\n", b[x], a[x]);
a_value = a[x];
b_value = b[x];
temp[b_value] = strings[a_value];
//If the values of a and b are equal to -1 denote the operation (end it)
if (b[x] == -1 && a[x] == -1) {
break;
}
}
//Get the swapped values
for (z = 0; z<=x; z++) {
printf("%s\n", temp[z]);
}
return 0;
}
Input:
aadf
bazz
abkt
bbaa
zzzz
1 3
0 4
3 2
-1 -1
The output is supposed to be like this
zzzz
bbaa
bazz
abkt
aadf
What I get is this
zzzz
bbaa
abkt
So everything works fine and strings get replaced based on that but the problem is that if b and a are not given so the swapped value should be the normal value and I don't know how to do it.
The task exactly says:
Read 5 strings from standard input and put them in an array. Each string is 4 characters length. Then prepare a function, that swaps i-th and j-th elements of the array. Indexes of i and j are given as standard input as two numbers separated by space. There can be more than one operations - each swap operation is separated by new line. Values -1 and -1 denote the end of operations. Print strings separated by new line.
Please help me and thanks ;)
Why 5D array, char strings[5][5][5][5][5] ? you just want to 5 strings as input So for this take 2D array
char strings[5][4];// total 5 strings , in each you can stores 4 characters.
Next things How will you scan data for 2D arrays, you need to rotate two loops namely for rows and columns . Assume row = 5 & col = 4
for (i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
scanf("%c", &strings[i][j]);
}
}
Next this temp[b_value] = strings[a_value]; statement you need to modify, modify according your requirement.
I hope this helps.
Here's you code, corrected and optimized:
#include <stdio.h>
#include <stdlib.h>
int main() {
char strings[5][5];
char temp[4];
int x,y;
//get 5 strings from the input
for (int i = 0; i < 5; i++) {
scanf("%s", strings[i]);
}
do{
scanf("%d %d", & x, & y);
// Assign string[x] to temp
for(int i=0;i<4;i++){
temp[i] = strings[x][i];
}
// Assign string[y] to string[x]
for(int i=0;i<4;i++){
strings[x][i] = strings[y][i];
}
// Assign temp to string[y]
for(int i=0;i<4;i++){
strings[y][i] = temp[i];
}
}while(x!=-1 && y!=-1);
//Get the swapped values
for (int i = 0; i< 5; i++) {
printf("%s\n", strings[i]);
}
return 0;
}
I have tested it. Let me know if you need any explanation.

Arrays printing sum of values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a problem with counting the sum of values from each line in a multidimensional array in the language C.
Example:
My array with values:
1 2 3 4
5 6 7 8
9 10 11 12
My result array should be:
10
26
42
w - count lines
k - count columns
int tab[w][k]; <-- this is a table just with values(it's example)
int sum[] = {0};
int i,j;
for(i=0;i<w;i++)
{
for(j=0;j<k;j++)
{
sum[i] = sum[i] + tab[i][j];
}
}
It doesn't work well. I've tried do it another way but it only counted the first row.
Please help me, thanks.
sum[] = {0} should be sum[w];, and you should fill it with zeroes before doing the sums.
Then just sum like you did, you can do it better using +=, that works the same as your original code but is easier to write:
#include <stdio.h>
#define w 3
#define k 3
int tab[w][k] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
int main()
{
int sum[w];
int i;
int j;
for (i = 0; i < w; i++)
{
sum[i] = 0;
}
for(i = 0 ; i < w ; i++)
{
for(j = 0 ; j < k ; j++)
{
sum[i] += tab[i][j];
}
printf("sum[%d] = %d\n", i, sum[i]);
}
}
Then your code should run fine

Why are the wrong array values being printed? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
For some reason, the nested for loop I have created at the bottom seems to be printing out the wrong first value (Gives me 3, when it should be 8). Yet, when I simply do printf (at the bottom), I am given the right value. Not really sure what's wrong with my code.
#include <stdio.h>
int main(void)
{
int d;
printf("Please input dimensions: (between 3 and 9, inclusive): \n");
scanf("%i", &d);
int array[d][d];
int k = 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
array[i][j] = (d * d) - k; //d^2 doesn't work to square a function
k++;
}
}
for (int z = 0; z < d; z++)
{
for (int y = 0; y < d; y++)
{
printf("%i\n", array[z][y]);
}
}
printf("%i\n", array[0][0]);
printf("%i\n", array[0][1]);
}
Edit: Sorry guys, the top value that was being printed was my own input. I was simply thinking it was the first value being printed.
Are you sure you aren't looking at your own input? I cut and paste and see:
Please input dimensions: (between 3 and 9, inclusive):
3
8
7
6
5
...
The 3 is actually what I typed and is echoed.

Error in sentences in bold: " Subscripted value is neither array nor pointer vector :(" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
#include <stdio.h>
void main()
{
int arrsize;
int randset;
int max;
int min;
int arr[arrsize];
int i, j;
int swap;
float mean;
printf("Input array size:\n");
scanf("%d", &arrsize);
printf("Input random set:\n");
scanf("%d", &randset);
printf("Input maximum possible value:\n");
scanf("%d", &max);
printf("Input minimum possible value:\n");
scanf("%d", &min);
// SORTING //
for (i = arrsize; i > 0; i--)
for(j = 1 ; j < i ; j++)
// Subscripted value is neither array nor pointer vector //
**if(arrsize[j-1] > arrsize[j])**
{
swap = arr[j];
arr[j] = arr[j-1];
arr[j-1] = swap;
}
for (i=0; i < arrsize; i++)
printf("%d", arr[i]);
printf("\n\n");
// MEAN //
for(i = 0 ; i < arrsize ; i ++)
{
//Subscripted value is neither array nor pointer vector //
**mean += arrsize[i];**
mean/= arrsize;
printf("The mean is %.2lf\n\n", mean);
}
// MEDIAN //
if(arrsize%2 == 0)
{
printf("The median is %.2lf", (float)(arr[arrsize/2 -1] + arr[arrsize/2]);
}
else
{
printf("The median is %d", arr [arrsize/2]);
printf("\n\n\n");
}
printf("The midrange is %.2lf\n\n", (float)(arr[0] + arr[arrsize - 1] / 2);
}
void fillintarray(int myarray [], int arrsize, int min, int max, unsigned int randset)
{
int iter;
srand (randset);
for (iter =0 ; iter < arrsize ; iter++)
{
myarray[iter] = rand() % (max - min + 1) + min;
}
}
Hi there! I put the lines where there were errors in bold, and the error was
"Subscripted value is neither array nor pointer vector"
This is an exercise for my ComSci class :) Help would be much appreciated! Thanks! :)
Well, the error message is pretty descriptive - you accessing a scalar variable int arrsize with subscript operator [] which could only be applied to array or pointer type.
You probably want to check j-1-th and j-th array elements, you should use
if (arr[j-1] > arr[j])
You're confusing between the array(arr) and its size(arrsize).
If you don't know the size of array , use malloc .
Use int *array = malloc(arraysize * sizeof(int));instead of int array[arraysize];
And put it afterscanf("%d",&arraysize);
And you also need init the array before use it.

A looping statement in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have this array and I can't seem to figure out how to form a for looping statement to be able to display these values in a list.
float array1[] = {30 , 0.81 , 0.16 , 30 , 2813 , 58 , 8.4 , 0.61};
I tried figuring it out but it just kept being a syntax error
for (array1; array1 <= 8; array1++ )
printf("%f\n", array1);
any suggestions??
It should be:
for(int i = 0; i < sizeof(array1)/sizeof(float); i++)
printf("%f\n", array1[i]);
array1 is the name of the array and not a variable that You want to increment!
int i = 0;
for (i = 0; i < 8; i++ ) printf("%f\n", array1[i]);
You need an index into your array:
int i;
for (i = 0; i < SIZE; ++i) { // SIZE should be 8.
printf("%f\n", array1[i]);
}
Think of your array as a contiguous section of memory that contains SIZE blocks of data. The index i is used to access the value stored in each block.
int i = 0;
for (i; i <= 8; i++)
printf("%f\n", array1[i]);
int length = sizeof(array1) / sizeof(float);
for(int i = 0; i< length; i++)
{
printf("%f\n", array1[i]);
}
try the following code,
float *ind;
for (ind=array1; ind < array1+8; ind++ ){
printf("%f\n", *ind)
}

Resources