-1

I want from the program to add 3 to elements which are greater than 3 and print them. It takes so much time that I couldn't see the result. Also, when I change n to 8 in loops directly, it gives a result; however, it's not related with what I want. How can I correct this code and improve that?

    #include <iostream>

using namespace std;

int main()
{
    int a[8] = {-5, 7, 1, 0, 3, 0, 5, -10};
    int b[8];
int n= sizeof(a);
for( int i=1; i<=n; i++) {
   if  (a[i]>3) {
    b[i] = a[i] + 3;
    }
   else {
    b[i]= a[i];
}

}
for (int i = 1; i <= n; i++)
    cout << b[i];
return 0;
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Ozgur Aydogmus
  • 113
  • 1
  • 1
  • 7

4 Answers4

2

The sizeof() function (int n = sizeof(a)) gives 32 because array 'a' contains 8 elements & each element is of 'int' type whose size is 4 byte in memory thats why it returns 32 in 'n' variable.so you must divide the value of 'n' with the size of integer.

Secondly the index of array starts with the zero '0' to one less than the length of array not with the 1 to length of array .

Try the below code ! I am also attach the output of the code .

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
    int a[8] = { -5, 7, 1, 0, 3, 0, 5, -10 };
    int b[8];
    int n = sizeof(a)/sizeof(int);

    for (int i = 0; i < n; i++) {
        if (a[i]>3) {
            b[i] = a[i] + 3;
        }
        else {
            b[i] = a[i];
        }

    }
    for (int i = 0; i < n; i++)
        cout << b[i]<<endl;

    return 0;
}

enter image description here

Usman
  • 1,983
  • 15
  • 28
2

The statement in your program int n=size(a) returns the total bytes occupied in memory for a. i.e int occupies 4 bytes and a is an array contains 8 elements so 8X4 = 32 .but while accessing the array elements using loop you are specifying i<=n meains i<=32 but there is only 8 elements but you are trying to access 32 elements which indicates that you are trying to access the elements more than 8.

exeutes the following code

#include <iostream>
using namespace std;
int main()
{
  int a[8] = {-5, 7, 1, 0, 3, 0, 5, -10};
  int b[8];
  int n=sizeof(a);
  cout<<"\n Value of n is : "<<n;
  return 0;
 }

Output Value of n is : 32

if you specify the exact number of array size your program will work properly.

#include <iostream>
using namespace std;
int main()
{
  int a[8] = {-5, 7, 1, 0, 6, 0, 8, -10};
  int b[8];
  for( int i=1; i<8; i++) 
     {
      if  (a[i]>3) 
         {
            b[i] = a[i] + 3;
         }
     else 
        {
           b[i]= a[i];
        }
     }
   cout<<"\n Values in a array";
   cout<<"\n -----------------\n";
   for (int i = 1; i <8; i++)
       cout << "\t"<<a[i];
   cout<<"\n Values in b array";
   cout<<"\n -----------------\n"; 
   for (int i = 1; i <8; i++)
       cout << "\t"<<b[i];
   return 0;
 }

OUTPUT

Values in a array                                                                                                                             
-----------------                                                                                                                             
  7       1       0       6       0       8       -10                                                                                              
Values in b array                                                                                                                             
---------------          
  10      1       0       9       0       11      -10                                          

I hope that you understand the concept.Thank you

K.Vani
  • 72
  • 4
1

Your int n = sizeof(a); doesn't works as you intend. I think you want to get the size of array (i.e. 8). But you gets the size in bytes of elements (e.g. 32, integer size or could be different depending of your system's architecture).

Change to int n = 8, will solve your problem.

Also note that for( int i=1; i<=n; i++) will get an "out of array" element.

Syscall
  • 19,327
  • 10
  • 37
  • 52
  • What function can I use instead of sizeof() to read length of the array? And code gives the correct result but they're all adjacent like -51010308-10. What should I do to see those as an array? – Ozgur Aydogmus Jan 20 '18 at 11:14
  • There is no function to do that in C. You are allocating the memory for your array, so, you know its size. Use a constant or allocate (and free) dynamically the memory. – Syscall Jan 20 '18 at 11:17
  • Add an extra space (or coma) to separate your values `cout << b[i] << " ";`. – Syscall Jan 20 '18 at 11:19
0

Sizeof function gives the value of the total bits of memory the variable occupy Here in array it stores 8 integer value that has 2bit size for each thus it returns 32